Regex in PHP

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi,

I have a tricky RegEx I am trying to get to the bottom of...

I want to look at the value of every 12th character, then backtrack and replace the LAST space I find with a \n. (So it backtracks from the 12th char back to the previous space and subs that) For example...

"The quick brown fox jumps over the lazy brown dog"

Becomes

"The quick\nbrown fox\njumps over\nthe lazy\nbrown dog"

The closest I can get is to do this:

if (strlen ($annotate)>11)
{
$annotate=preg_replace("/(.*)?( )/","$0\n",$annotate);
}

But this only does the last space of each string fed into it. I know using the if statement is the wrong approach, but I'm very stuck here.

Any help greatly appreciated

Thanks
 
Last edited:
PHP:
<?php
function splitBySpaces($str,$maxChars = 12){
    $str = str_split($str,$maxChars);
    $out = '';
    foreach ($str as $s){
        $strrpos = strrpos($s,' ');
        if ($strrpos){
            $out .= substr_replace(
                $s,
                "\n",
                strrpos($s,' '),
               1 
            );
        } else {
            $out .= $s;
        }
    }
    return $out;
}
$str = 'The quick brown fox jumps over the lazy brown dog';
echo splitBySpaces($str);
?>
 
wow that is fantastic- will give that a go in a bit Thanks!

Oft quoted: "When faced with a problem, many developers think 'I know, I'll use a Regex for that', now they have two problems".

My rule of thumb is that if I cannot figure out a regex within a couple of minutes it should probably be done in code instead. :D
 
hehe. I think the main problem using regex for that is I couldn't find a way to count the nth occurrence of something.

My rule of thumb is that if I can't achieve what I'm trying to (whilst remaining relatively un-convoluted) with the str functions, I see what regex can do. I've always been under the impression that str functions are much quicker than preg/ereg, so I try to use them when I can.

I'd be interested to see if one of the regex gods can come up with a one-liner for this, though. I tried and didn't get very far.
 
Regex makes my eyes bleed, but got to say it helps in a lot of situations.
Can anyone suggest websites / books that may help out people like me?!

Cheers
 
PHP:
<?php
function splitBySpaces($str,$maxChars = 12){
    $str = str_split($str,$maxChars);
    $out = '';
    foreach ($str as $s){
        $strrpos = strrpos($s,' ');
        if ($strrpos){
            $out .= substr_replace(
                $s,
                "\n",
                strrpos($s,' '),
               1 
            );
        } else {
            $out .= $s;
        }
    }
    return $out;
}
$str = 'The quick brown fox jumps over the lazy brown dog';
echo splitBySpaces($str);
?>
Hi,

I spent an hour or 2 looking over your code... if I give it the string "Bes OCUK" it still linebreaks but I can't figure out why? :( Any ideas why? as I would expect it to all be on one line.

Thanks
 
Back
Top Bottom