PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Sep 25, 2020 12:05 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Replace all...
PostPosted: Wed Aug 16, 2006 12:31 am 
Offline
Forum Contributor
User avatar

Joined: Tue Aug 10, 2004 3:08 am
Posts: 424


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 1:41 am 
Offline
Site Administrator
User avatar

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
If you know the linkid will ways be numeric, try this

Syntax: [ Download ] [ Hide ]
$str = preg_replace('#\[linkid:([0-9]*)\]#', "<a href=\"page.php?id=1\">", $str);


Or for alphanumerics, try
Syntax: [ Download ] [ Hide ]
$str = preg_replace('#\[linkid:([A-Za-z0-9]*)\]#', "<a href=\"page.php?id=1\">", $str);


This is way untested...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 2:10 am 
Offline
Forum Contributor
User avatar

Joined: Tue Aug 10, 2004 3:08 am
Posts: 424
Thanks for that. It wasn't exactly what I wanted but atleast the regex was helpful... I figured otu what I want but the str_replace doesn't seem to be working... Any ideas?


Syntax: [ Download ] [ Hide ]
$match_count = preg_match_all("#\[linkid:([0-9]*)\]#si", $layout, $matches);

for ($i = 0; $i < $match_count; $i++) {

        $linkid = $matches[1][$i];

        $linkid_info = mysql_fetch_array(mysql_query("select * from {$tbl_name}pages where id='$linkid'"));



        $layout = str_replace("[linkid:{$linkid}]", "index.php?page={$linkid_info[slug]}", $layout);

        $linkid = "";

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 12:19 am 
Offline
Forum Contributor
User avatar

Joined: Tue Aug 10, 2004 3:08 am
Posts: 424
Is it really that difficult? Absolutley everything works except the str_replace part... Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 3:41 am 
Offline
DevNet Resident
User avatar

Joined: Tue Mar 16, 2004 11:03 am
Posts: 1425
Location: Aachen, Germany
What happens when you do a print_r($matches) ?

You may find to be better than a for loop.
In the SQL is linkid an integer or varchar column? If integer get rid of the quotes.
Syntax: [ Download ] [ Hide ]
$match_count = preg_match_all("#\[linkid:([0-9]*)\]#si", $layout, $matches);

if (!empty($matches[1])) {

   foreach($matches[1] as $linkid) {

         $linkid_info = mysql_fetch_array(mysql_query("select * from {$tbl_name}pages where id=$linkid"));

         $layout = str_replace("[linkid:{$linkid}]", "index.php?page={$linkid_info[slug]}", $layout);

   }

} else {

  echo("TEMP DEBUG No Matches");

}


This may work but not tested.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group