esiason14 Posted September 1, 2006 Share Posted September 1, 2006 I'm just trying to match something like this:[code][url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url][/code]and replace it with[code]<a href="http://www.mydomain.com/whatever.php">Read more...</a>[/code]any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/ Share on other sites More sharing options...
effigy Posted September 1, 2006 Share Posted September 1, 2006 [code]<?php $bbcode = '[url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url]'; echo preg_replace('%\[url=([^]]+)\]([^[]+)\[/url\]%', '<a href="\1">\2</a>', $bbcode);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-84024 Share on other sites More sharing options...
esiason14 Posted September 5, 2006 Author Share Posted September 5, 2006 Hi effigy,Thanks for the response. That does work perfectly. One followup question though:What if there is a block of text prior to the url that I want to replace. how can I match that and show it along with the new, reformatted link. The block of text is always followed by a double line break[code]A whole bunch of text here. Blah blah. SDSdsdsad. [url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url][/code]and replace it with[code]A whole bunch of text here. Blah blah. SDSdsdsad. <a href="http://www.mydomain.com/whatever.php">Read more...</a>[/code]any help would be appreciated[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86086 Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 The same code will work since it lacks BOL (beginning of line) or EOL (end of line) anchors. Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86144 Share on other sites More sharing options...
esiason14 Posted September 5, 2006 Author Share Posted September 5, 2006 This is what I have:[code]echo "<br><table align=\"center\" width=\"500\"><tr bgcolor=\"#000066\"><td style=\"font-family: Arial,Helvetica,sans-serif; font-weight: bold; font-size:12px; color:#ffffff; text-decoration:none; \"> $firstname $lastname in the news</td></tr>"; for($i=0;$i<$count;$i++) { $bbcode = "[url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url]"; $newurl = preg_replace('%\[url=([^]]+)\]([^[]+)\[/url\]%', '<a href="\1">\2</a>', $bbcode);echo "<tr><td><b><u>".$title[$i]."</u></b></td></tr><tr><td>".$newurl."</td></tr>";}echo "</table>";[/code]For a post that looks like this, which I'm trying to match:[code]Colts QB Peyton Manning said he likes the team's decision to draft Joseph Addai to help replace Edgerrin James. "I'm biased toward Southeastern Conference players. I think he's been well battle-tested," Manning said. "He's going to be able to come in and help us. Dominic Rhodes will play as well, but everybody else is going to have to raise their level of play."Read more...[/code]..returns this:[code]Manning likes addition of Addai (this is the title of the post)Read more...[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86151 Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 The code is always replacing the string[tt] [ url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url ] [/tt] as instructed; I'm not sure where your other information comes into play. Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86165 Share on other sites More sharing options...
esiason14 Posted September 5, 2006 Author Share Posted September 5, 2006 I'm actually trying to match the text before AND the "read more" link.....not just the link. Currently, I can return only the "Read more" link with what you posted before. The whole thing that I am trying to match looks like this. [code]In two preseason games, Carson Palmer (knee) went 13-of-20 for 173 yards and four touchdowns, leaving him with a gaudy passer rating of 131.9. Even though he's still working some out some kinks, he's good enough at this point to have the Bengals thinking of another title. "We were fortunate to finish out the preseason undefeated," Palmer said. "Hopefully this momentum will carry us through the first four games, into the bye, and on through the rest of the season."<br /><br /><a href="http://mydomain.com/players/playerpage/396173/rss" target="_blank" class="postlink">Read more...</a>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86439 Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 Whatever you want to match against, use it as your match variable. As I mentioned before, you're only matching against an url string in your example.If you change [b]$bbcode[/b] from:[code][url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url][/code]to:[code]In two preseason games, Carson Palmer (knee) went 13-of-20 for 173 yards and four touchdowns, leaving him with a gaudy passer rating of 131.9. Even though he's still working some out some kinks, he's good enough at this point to have the Bengals thinking of another title. "We were fortunate to finish out the preseason undefeated," Palmer said. "Hopefully this momentum will carry us through the first four games, into the bye, and on through the rest of the season."<br /><br />[url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url][/code]You should get the desired result. Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86471 Share on other sites More sharing options...
esiason14 Posted September 5, 2006 Author Share Posted September 5, 2006 Ok, I gotcha. I'm sorry to be a pain, but the text is different everytime. How would I match the text between the <span class="postbody"> and the first </ br> tag. I promise this is my last question on this. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86481 Share on other sites More sharing options...
effigy Posted September 5, 2006 Share Posted September 5, 2006 [code]preg_match('%(?<=<span class="postbody">)(.+?)(?=</ br>)%s', $string, $matches);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86491 Share on other sites More sharing options...
esiason14 Posted September 6, 2006 Author Share Posted September 6, 2006 Thanks, effigy. Everything is working great. You da man! Quote Link to comment https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-87426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.