PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
azezal
Forum Newbie
Posts: 2 Joined: Sat Oct 29, 2005 4:58 am
Post
by azezal » Sat Oct 29, 2005 5:02 am
Jcart | Please use 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Sat Oct 29, 2005 5:12 am
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);
azezal
Forum Newbie
Posts: 2 Joined: Sat Oct 29, 2005 4:58 am
Post
by azezal » Sat Oct 29, 2005 3:10 pm
Jcart | Please use 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.
'
IsCode: 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Sun Oct 30, 2005 8:35 am
but it works fine for me o_O can you show me your code
yum-jelly
Forum Commoner
Posts: 98 Joined: Sat Oct 29, 2005 9:16 pm
Post
by yum-jelly » Sun Oct 30, 2005 5:12 pm
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