ThePrimeagen
ThePrimeagen

Reputation: 4582

php and regular expressions

Ok i have tried this quite a few times now. I still cannot figure it out.

Thisis my problem. I want to take any <br><br> and turn it into <br>

So this is what i do

$test = ereg_match("/<br><br>/", "<br>", "<br><br>Test<br><br>"); echo $test;

It produces $test == "Test" rather than $test = "<br>Test<br>"

Any help? Then i would like to expand it so that any <br\s*\/{0, 1}>\s*<br\s*\/{0, 1}> So that any two seperated by space can be turned into 1. Any help?

Upvotes: 2

Views: 101

Answers (8)

Phoenix
Phoenix

Reputation: 4536

Use str_replace where you can. Given your comments on stuff such as wanting to replace <br><br><br><br><br><br><br><br><br><br><br> with just 1 <br>, do this:

while(strpos($inputstring,'<br><br>'))  //while <br><br> can be found in string
{
    $inputstring = str_replace('<br><br>', '<br>', $inputstring);  //replace <br><br>
}

PHP has a bad implementation of regular expressions, and they take a much much longer time to do in PHP than it's string manipulations. Like a whole greater order of magnitude time longer.

To catch various different potential issues with the tags, you can do:

$searchArray = array('<br><br>','<br /><br />','other potential spellings');
while(stripos($inputstring,$searchArray))  //while case insensitive search strings are in string
{
    $inputstring = str_ireplace($searchArray, '<br />', $inputstring);  //replace case insensitive strings
}

You can also use strip_tags just to remove any unwanted tags all together:

$inputstring = strip_tags($inputstring, '<p><a><b><i><other allowed tags>');

But, of course, this would remove all <br> tags.

Upvotes: 0

ridgerunner
ridgerunner

Reputation: 34435

Exactly 2 BR separated by optional whitespace:

$str = preg_replace('/<br[^>]*>(?:\s*<br[^>]*>){1}/', '<br>', $str);

2 or more BR separated by optional whitespace:

$str = preg_replace('/<br[^>]*>(?:\s*<br[^>]*>){1,}/', '<br>', $str);

Upvotes: 0

ThePrimeagen
ThePrimeagen

Reputation: 4582

Found the answer. I am so stupid.

In a seperate file i had

define("ALLOWED_TAGS", ""); ... ... ... define("ALLOWED_TAGS", "my tags blah blah blah");

Sorry for wasting everyones time. GG

Upvotes: 0

Justin Morgan
Justin Morgan

Reputation: 30700

As Mike Lewis says, use string manipulation if you don't need to account for variation in the input string. On the other hand, if you want to collapse any multiple <br> or <br/> tags into a single tag, allowing for whitespace inside them, this should work for you:

$result = preg_replace('#(<\s*br\s*(/\s*)?>){2,}#', '<br>', $subject);

I'm not totally sure why your version isn't working, but if the above doesn't work, there's probably something else going on here.

Upvotes: 0

anubhava
anubhava

Reputation: 786349

Can you try this:

preg_replace("#<br[^>]*><br[^>]*>#", "<br>", "<br><br>Test<br><br>");

Both these lines produce same output:
var_dump(preg_replace("#<br[^>]*><br[^>]*>#", "<br>", "<br><br>Test<br><br>"));
var_dump(preg_replace("#<br[^>]*><br[^>]*>#", "<br>", "<br /><br>Test<br><br>"));

OUTOUT: string(12) "<br>Test<br>"

Upvotes: 0

Mike Lewis
Mike Lewis

Reputation: 64177

You can just use str_replace

<?php

$str = "<br><br>Test<br><br>";

echo str_replace("<br><br>", "<br>", $str);

Update

Example:

http://codepad.org/MWzg81Qy

To replace 2 or more br's(with unlimited spacing)... you can do preg_replace:

<?php

$str = "<br><br     >    <br><br>Test<br     ><br>";

echo preg_replace("/(\s*<br\s*>)+/", "<br>", $str);

http://codepad.org/CrltyhSs

Upvotes: 2

mario
mario

Reputation: 145512

You most certainly have a typo in your function names. The preg_* functions have mostly replaced the old ereg_ functions.

Your second regex should be written as:

print preg_replace("#<br\s*/?>\s*<br\s*/?>#", "<br>", "<br><br>Test");

The ? is shorthand for your {0,1}, and by using # as delimiter you spare the extraneous escaping of forward slashes.

Upvotes: 0

John Parker
John Parker

Reputation: 54445

Perhaps not the answer you're after but using str_replace will be considerably more efficient than a regular expression.

i.e.: $test = str_replace('<br><br>', '<br>', $sourceString);

Incidentally, I'd be tempted to replace the <br> tags with <br /> in this day and age as well. :-)

Upvotes: 0

Related Questions