Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-05-2011, 12:40 PM preg_replace links
Extreme Talker

Posts: 176
Trades: 0
this probably get's asked a lot, but I'm having trouble understanding preg_replace()


I want to turn @post-31 into <a href="#post-31">@post-31</a>



this is what I've got, obviously doesn't work
PHP Code:
$text preg_replace('/^@post-[0-9]/''<a href=""></a>'$post_data['text']); 
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-05-2011, 01:56 PM Re: preg_replace links
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
PHP Code:
$text preg_replace('/^@post-([0-9]+)$/''<a href="#post-$1">@post-$1</a>'$post_data['text']); 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-05-2011, 02:03 PM Re: preg_replace links
Extreme Talker

Posts: 176
Trades: 0
Quote:
Originally Posted by mgraphic View Post
PHP Code:
$text preg_replace('/^@post-([0-9]+)$/''<a href="#post-$1">@post-$1</a>'$post_data['text']); 
thanks! though still seems to need some work, only works with 1 digit numbers?
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 02-05-2011, 02:07 PM Re: preg_replace links
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by Skeddles View Post
thanks! though still seems to need some work, only works with 1 digit numbers?
Using the + will capture one or more in the number set, so it will capture 9 or 99 or 999999. The replace string will replace the $1 with the captured value.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-05-2011, 02:26 PM Re: preg_replace links
Extreme Talker

Posts: 176
Trades: 0
Quote:
Originally Posted by mgraphic View Post
Using the + will capture one or more in the number set, so it will capture 9 or 99 or 999999. The replace string will replace the $1 with the captured value.
well in my code it appears to only work with 1, @post-4 get's linked, @post-47 does not. EDIT: or something else must be wrong... now nothing works, it worked only on one entry, even if i had the same text on others, and then I deleted it and now none work... this is weird.
PHP Code:
<?php

$text 
preg_replace('/^@post-([0-9]+)$/''<a href="#post-$1">@post-$1</a>'"@post-4 @post-0 @post-22 @post-2 @post-12 @post-2 @post-21");

echo 
$text;

?>
doesn't work

Last edited by Skeddles; 02-05-2011 at 02:39 PM..
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 02-05-2011, 03:17 PM Re: preg_replace links
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
OK, I didn't realize you would have a string with multiple matches, using the ^ and $ in the pattern string was only matching the first set.

PHP Code:
$text preg_replace('/@post-([0-9]+)/''<a href="#post-$1">@post-$1</a>''@post-4 @post-0 @post-22 @post-2 @post-12 @post-2 @post-21'); 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-05-2011, 03:59 PM Re: preg_replace links
Extreme Talker

Posts: 176
Trades: 0
Quote:
Originally Posted by mgraphic View Post
OK, I didn't realize you would have a string with multiple matches, using the ^ and $ in the pattern string was only matching the first set.

PHP Code:
$text preg_replace('/@post-([0-9]+)/''<a href="#post-$1">@post-$1</a>''@post-4 @post-0 @post-22 @post-2 @post-12 @post-2 @post-21'); 
oh cool, works, thank you
Skeddles is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to preg_replace links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24682 seconds with 12 queries