Forum Moderators: coopster

Message Too Old, No Replies

quick preg problem

         

FiRe

6:08 pm on Nov 24, 2006 (gmt 0)

10+ Year Member



I am trying to replace anything that looks like this:

HREF="function.blah.php"

into just:

HREF="blah"

I tried this:

$body = preg_replace("/HREF=\"function.(.*).php\"/is", "href='\\1'", $body);

But not having much success! any help?

[edited by: FiRe at 6:09 pm (utc) on Nov. 24, 2006]

Psychopsia

8:10 pm on Nov 24, 2006 (gmt 0)

10+ Year Member



Hi! Try this:

$body = preg_replace('/href="function\.([a-z0-9]+)\.php"/is', 'href="\\1"', $body);

[edited by: Psychopsia at 8:10 pm (utc) on Nov. 24, 2006]

StupidScript

8:38 pm on Nov 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Psychopsia's note is good. I figured at least change to
$body = preg_replace("/HREF=\"function\.(.*)\.php\"/is", "href=\"\\1\"", $body);
. In my tests it worked fine. Here's my test code, just in case
$body
is being defined differently:

$body='a href="function.blah.php"';
echo $body."<br />";
$body = preg_replace("/HREF=\"function\.(.*)\.php\"/is", "href=\"\\1\"", $body);
echo $body."<br />";

Check the use of single- and double-quotes.

[edited by: StupidScript at 8:39 pm (utc) on Nov. 24, 2006]

Psychopsia

12:33 am on Nov 25, 2006 (gmt 0)

10+ Year Member



I changed the regex to ([a-z0-9]+) because (.*) returns the following:

<a href="aaa.php"></a><a href="function.bbb.php"></a><a href="function.ccc.php"></a><a href="function.ddd"></a>

Another way to get the correct result, is use (.*?)