Regex Help – PHP

  php

Q(Question):

Wow, it’s been a long time since I’ve had to ask for that, but this has me
stumped. It’s probably going to be something simple I’m overlooking, in
which case I shall properly beat myself in the forehead with a board while
chanting sonorously, but meanwhile…

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = ‘|||||’;
$x = preg_replace(‘=\| *\|=U’, ‘|_|’, $x);

Result:

|_||_||

What I need is:

|_|_|_|_|

Any suggestions?


Alan Little
Phorm PHP Form Processor
http://www.php-form.net/

A(Answer):

Alan Little escribió:

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = ‘|||||’;
$x = preg_replace(‘=\| *\|=U’, ‘|_|’, $x);

Result:

|_||_||

What I need is:

|_|_|_|_|

Ugly and not fully tested, but I hope it can give you an idea:

<?php

$x = ‘|||||’;

$substrings = explode(‘|’, $x);
foreach($substrings as &$i){
if( preg_match(‘/^\s*$/i’, $i) ){
$i = ‘_’;
}
}

echo substr(implode(‘|’, $substrings), 1, -1);

?>


http://alvaro.es – Álvaro G. Vicario – Burgos, Spain
— Mi sitio sobre programación web: http://bits.demogracia.com
— Mi web de humor al baño María: http://www.demogracia.com

A(Answer):

On Thu, 17 Apr 2008 17:50:04 +0200, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote:

Wow, it’s been a long time since I’ve had to ask for that, but this has
me
stumped. It’s probably going to be something simple I’m overlooking, in
which case I shall properly beat myself in the forehead with a board
while
chanting sonorously, but meanwhile…

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing
or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = ‘|||||’;
$x = preg_replace(‘=\| *\|=U’, ‘|_|’, $x);

Result:

|_||_||

so it finds 1 & 3, not 2 & 4.

What I need is:

|_|_|_|_|

Any suggestions?

<?php
$x = ‘|||||’;
$x = preg_replace(‘/\|(\s*)(?=\|)/’, ‘|_’, $x);
echo $x;
?>

Rik Wasmus

A(Answer):

On Thu, 17 Apr 2008 10:50:04 -0500, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote in
<Xn**************************@216.196.97.131>:

>Wow, it’s been a long time since I’ve had to ask for that, but this has me
stumped. It’s probably going to be something simple I’m overlooking, in
which case I shall properly beat myself in the forehead with a board while
chanting sonorously, but meanwhile…

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = ‘|||||’;
$x = preg_replace(‘=\| *\|=U’, ‘|_|’, $x);

Are you sure that PRCE is enabled (to make the ‘U’ affect the
pattern’s greediness)?

>Result:

|_||_||

What I need is:

|_|_|_|_|

Any suggestions?

This is less elegant, but should work:

$x = ‘||||||’;
$x = preg_replace(‘/\|/’, ‘|_’, $x);
$x = substr($x, 0, strlen($x) – 1);

With something this simple, you could also use str_replace. Of course
if you’ve wildly simplified the code for demo purposes, this
suggestion may not be viable. I mention it because sometimes the
simplest solution eludes us. 🙂

Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research

A(Answer):

Carved in mystic runes upon the very living rock, the last words of Rik
Wasmus of comp.lang.php make plain:

On Thu, 17 Apr 2008 17:50:04 +0200, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote:

>I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have
nothing or only spaces between them. However, if I have consecutive
occurrences of this pattern, it only finds 1 and 2, but not 2 and 3.
To illustrate:

$x = ‘|||||’;
$x = preg_replace(‘=\| *\|=U’, ‘|_|’, $x);

Result:

|_||_||

so it finds 1 & 3, not 2 & 4.

>What I need is:

|_|_|_|_|

Any suggestions?

<?php
$x = ‘|||||’;
$x = preg_replace(‘/\|(\s*)(?=\|)/’, ‘|_’, $x);
echo $x;
?>

Thanks, that works. Now I’ll have to sit down and figure out why. 🙂


Alan Little
Phorm PHP Form Processor
http://www.php-form.net/

A(Answer):

Carved in mystic runes upon the very living rock, the last words of
Charles Calvert of comp.lang.php make plain:

This is less elegant, but should work:

$x = ‘||||||’;
$x = preg_replace(‘/\|/’, ‘|_’, $x);
$x = substr($x, 0, strlen($x) – 1);

With something this simple, you could also use str_replace. Of course
if you’ve wildly simplified the code for demo purposes, this
suggestion may not be viable. I mention it because sometimes the
simplest solution eludes us. 🙂

Thanks, but I need it to replace only zero or more spaces between the bars
with the underscore. A better example:

$x = ‘| | abc | | x||’;

Should yield:

|_| abc |_| x|_|

Rik’s pattern does it; I’ve just never learned about assertions.


Alan Little
Phorm PHP Form Processor
http://www.php-form.net/

A(Answer):

*** Alan Little escribió/wrote (Thu, 17 Apr 2008 13:57:45 -0500):

><?php
$x = ‘|||||’;
$x = preg_replace(‘/\|(\s*)(?=\|)/’, ‘|_’, $x);
echo $x;
?>

Thanks, that works. Now I’ll have to sit down and figure out why. 🙂

The (?=….) part is a lookahead assertion. From manual:

"An assertion is a test on the characters following or preceding the
current matching point that does not actually consume any characters"

I admit I had never dived so deep in the Pattern Syntax manual page <:-)

http://alvaro.es – Álvaro G. Vicario – Burgos, Spain
— Mi sitio sobre programación web: http://bits.demogracia.com
— Mi web de humor en cubitos: http://www.demogracia.com

LEAVE A COMMENT