Hi all,

Here is a version that should not fail when encountering punctuation.

```PHP
    #!/usr/bin/env php
    <?php
    //---
    function jj_ucwords($string) {
        return preg_replace_callback('/\b(\w)/', function ($matches) { 
return strtoupper($matches[0]); }, $string);
    }
    //---
    function capitalize_skipping_inner_tags($string) {
        $array = preg_split('/(<[^>]+?>)/iusx', $string, -1, 
PREG_SPLIT_DELIM_CAPTURE);
        $m = count($array);
        $i = 0;
        while($i < $m) {
            if (strpos($array[$i], '<') === 0) {
                $i++;
            } else {
                $array[$i] = jj_ucwords(strtolower($array[$i]));
            }
            $i++;
        }
        return implode($array);
    }
    //---
    $input = file_get_contents('php://stdin');
    $split_pattern = '/(<a[^>]+?>|<\/a>)/iusx';
    $array = preg_split($split_pattern, $input, -1, 
PREG_SPLIT_DELIM_CAPTURE);
    $m = count($array) - 1 ;
    $i = 0;
    while($i < $m) {
        if (stripos($array[$i], '<a') === 0) {
            $i++;
            $array[$i] = capitalize_skipping_inner_tags($array[$i]);
        }
        $i++;
    }
    $output = implode($array);
    file_put_contents('php://stdout', $output);
    exit(0);
    //---
```

And another version using NodeJS:

```JavaScript
    #!/usr/bin/env node

    process.stdin.resume();
    process.stdin.setEncoding('utf8');

    // Capitalize words but leave inner tags untouched.
    function capitalize(text) {
        const splits = text.split(/(<[^>]+?>)/i);
        const n = splits.length;
        let i = 0;
        while (i < n) {
            if (!splits[i].startsWith('<')) {
                splits[i] = splits[i].toLowerCase().replace(/\w\S*/g, (w) 
=> (w.replace(/^\w/, (c) => c.toUpperCase())));
            }
            i++
        }   
        return splits.join('');
    }

    process.stdin.on('data', function(text) {
        const splits = text.split(/(<a[^>]+?>|<\/a>)/i);
        const n = splits.length - 1; // Last item is itself an closing 
anchor tag of not inside an anchor. Hence, no need to capitalize it.
        let i = 0;
        while (i < n) {
            if (splits[i].toLowerCase().startsWith('<a')) {
                i++;
                splits[i] = capitalize(splits[i]);
            }
            i++
        }   
      process.stdout.write(splits.join(''));
    })
```

BTW does anyone know how to add code highlighting to this posts ?

Best regards,

Jean Jourdain


-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/9dcf7b22-d30c-4bc3-b801-b08853fb390fn%40googlegroups.com.

Reply via email to