Page 1 of 1

preg_replace help

Posted: Sat Oct 29, 2005 5:02 am
by azezal
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have been trying to replace this, but just cant get it.

Code: Select all

<?php
echo preg_replace("/main.php?subaction=showcomments&id\=(.*?)\&archive=&start_from=&ucat=&page=contentte.php/",
             "\\1",
             $body);
?>

I am trying to replace:

main.php?subaction=showcomments&id=1234153463&archive=&start_from=&ucat=&page=contentte.php

with just the 1234153463 from id=1234153463


I want it to go through the entire body of the page and do this.


I have been trying to learn how to use preg_replace, but there are so many different ways that im pretty confused.


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Oct 29, 2005 5:12 am
by n00b Saibot
you forgot to excape the ? ;)

Code: Select all

$Pattern = "#main.php\?subaction=showcomments&id=(.*?)&archive=&start_from=&ucat=&page=contentte.php#";
echo preg_replace($Pattern, "\\1", $body);

Posted: Sat Oct 29, 2005 3:10 pm
by azezal
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I still can't get it to work.

nothing happenes.
'
Is

Code: Select all

<?php
$Pattern = "#main.php\?subaction=showcomments&id=(.*?)&archive=&start_from=&ucat=&page=contentte.php#";
echo preg_replace($Pattern, "\\1", $body); 
?>
The page generates this on it exactly:
<a href="/main.php?subaction=showcomments&id=1130446722&archive=&start_from=&ucat=&page=content.php">

and I am try to make it:
<a href="1130446722">


thanks for the help, but I think I should ahve told you about the entire string and not just that part, sorry.


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Oct 30, 2005 8:35 am
by n00b Saibot
but it works fine for me o_O can you show me your code

Posted: Sun Oct 30, 2005 5:12 pm
by yum-jelly
Something like this should work!

Code: Select all

<?php

$old = '#(\<.*a.*href=).*id=\b([0-9]+)\b.*(\/?\>)#i';

$new = '\\1"\\2"\\3';

echo preg_replace ( $old, $new, $str );

?>
yj