Shadowing Posted July 28, 2012 Share Posted July 28, 2012 Hey guys i think i must be missing something. Trying to do this with less code possible Was looking over string functions in the manual but i dont see anything that does what im trying to do. $string = "hey whats up @sam how are you doing"; I'm wanting to replace @sam with <a href='' class='profile_name' id='$name>$name</a> is there a way to extract @sam so i can get rid of the @ then place the name into the link $name the only thing i can find is finding the location of @ using strpos but im not sure how to extract only the letters attached to @ and nothing else in the string only way i can think about doing it is to extract everything after @ then exploding it by spaces with a limit of 1 is that the best way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/ Share on other sites More sharing options...
Tarential Posted July 28, 2012 Share Posted July 28, 2012 Try using a regular expression to match the at symbol followed by any characters [edit: plural] that aren't a space followed by a space: /@([^\s]+)\s/ Use this with a function like preg_match to find the name. Note that the brackets around the part between the @ and the following space indicate a part you wish to save for use. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365093 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 preg_replace would save a step. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365096 Share on other sites More sharing options...
Shadowing Posted July 28, 2012 Author Share Posted July 28, 2012 thanks for the responce Tarential expressions are like a dark art to me lol Thanks alot it works. i'll go ahead and just submit my code incase someone else reads this to help them out. $string = "hey whats up @sam how are you doing"; if (preg_match('/@([^\s]+)\s/', $string, $name)) { print_r($name); } Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365097 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 Either use regular expression or use the str_replace() function. That takes a search term, a replacements term and the string you want to work on. I've heard it's quicker than using preg_replace with regex but it's probably such a nominal difference that it's not worth worrying about. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365098 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 Either use regular expression or use the str_replace() function. That takes a search term, a replacements term and the string you want to work on. I've heard it's quicker than using preg_replace with regex but it's probably such a nominal difference that it's not worth worrying about. The difference in speed is huge, but str_replace will only match static data. Regular expressions allow for patterns to be found. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365099 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 Ahh well there you go And yeah I would favour regex if looking for something dynamic but the post seemed to suggest @sam was the term - so maybe i misread Either use regular expression or use the str_replace() function. That takes a search term, a replacements term and the string you want to work on. I've heard it's quicker than using preg_replace with regex but it's probably such a nominal difference that it's not worth worrying about. The difference in speed is huge, but str_replace will only match static data. Regular expressions allow for patterns to be found. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365101 Share on other sites More sharing options...
Shadowing Posted July 28, 2012 Author Share Posted July 28, 2012 ya the name is dynamtic im confused on how i would use preg_replace. How would i get the variable $name filled in this link with sam using preg_replace instead of preg_match? You saying this could be done in one line of code xyph? <a href='' class='profile_name' id='$name>$name</a> $string = "hey whats up @sam how are you doing"; if (preg_match('/@([^\s]+)\s/', $_POST['message'], $name)) { print_r($name); } Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365102 Share on other sites More sharing options...
Tarential Posted July 28, 2012 Share Posted July 28, 2012 I believe he was suggesting using preg replace on a regex like this to cut out everything except the matching string: /^.*@([^\s]+)\s.*$/ Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365105 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 Probably not the most efficient way of doing it but it works... $string = "hey whats up @sam how are you doing"; $pattern = '/@[a-zA-Z]+/'; preg_match($pattern, $string, $matches); $name = substr($matches[0], 1); $stringtwo = "<a href='' class='profile_name' id='name'>$name</a>"; //$string contains new string with link changed to name dynamically $string = str_replace($matches[0], $stringtwo, $string); Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365108 Share on other sites More sharing options...
Shadowing Posted July 28, 2012 Author Share Posted July 28, 2012 when I use the code im using now the return of the array is Array ( [0] => @sam[1] => sam) so im already getting just the name Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365109 Share on other sites More sharing options...
Shadowing Posted July 28, 2012 Author Share Posted July 28, 2012 how would i go about changing it so its looking for " @" instead of "@" this way people can still type email address's and it will ignore it Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365112 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 Just need to change the pattern to: '/\s@[a-zA-Z]+/'; Oh and add a space before $name in $stringtwo $stringtwo = "<a href='' class='profile_name' id='name'> $name</a>"; how would i go about changing it so its looking for " @" instead of "@" this way people can still type email address's and it will ignore it Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365113 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 OH and you'd also have to change $name = substr($matches[0], 2); Because now it needs to look two characters from the start instead of one. Then it should all work again! Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365115 Share on other sites More sharing options...
Tarential Posted July 28, 2012 Share Posted July 28, 2012 Adding "\s" in front of the @ symbol in the regex is the correct way to include a space in the match. Then, assuming you used my code, you would reference $matches[1] (not $matches as a whole, which is an array, or $matches[0], which is the entire string match). $matches[1] is what contains "sam" in the array you posted. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365117 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 The manual provides details on how to do this is preg_replace. I'm not sure why I need to post an example as well. Also \s is the correct way to match a white-space character. A literal space, or one preceded by a backslash in the case of free-spacing, is the correct way to match a space. <?php $string = 'hello @sam nice to meet you'; $match = '/@([^\s]+)/'; $replace = '<a href="page.php?name=$1">$1</a>'; echo preg_replace($match, $replace, $string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365120 Share on other sites More sharing options...
Christian F. Posted July 28, 2012 Share Posted July 28, 2012 Tarential: No need to bind the RegExp to the start and end of the string. We're not talking about validating the entire string, but just looking to grab a little subset of it. Shadowing: To sum up this little thread, what you'll need is something that looks like this: $string = "hey what's up @sam how are you doing?"; $replace = ' <a href="" class="profile_name" id="$1">$1</a>'; echo preg_replace ("/\\s@(\\S+)/u", $replace, $string); Double escaping since "\" is a meta-character in PHP strings. "\\S" is the same as "[^\\s]+", only with less typing. Also using the "u" modifier to make it UTF-8 compatible. Also, added a space in front of the anchor-tag, to replace the one we're removing in the RegExp. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365121 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 Tarential: No need to bind the RegExp to the start and end of the string. We're not talking about validating the entire string, but just looking to grab a little subset of it. Shadowing: To sum up this little thread, what you'll need is something that looks like this: $string = "hey what's up @sam how are you doing?"; $replace = ' <a href="" class="profile_name" id="$1">$1</a>' echo preg_replace ("/\\s@(\\S+)/u", $string, $replace); Double escaping since \ is a meta-character in PHP strings. "\\S" is the same as "[^\\s]+", only with less typing. Also using the "u" modifier to make it UTF-8 compatible. You should test your code. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365124 Share on other sites More sharing options...
Christian F. Posted July 28, 2012 Share Posted July 28, 2012 Yeah, noticed it right after posting. Changed it while you were posting. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365125 Share on other sites More sharing options...
Drongo_III Posted July 28, 2012 Share Posted July 28, 2012 Out of interest so i understand this patttern (as my regex is a bit rusty): $match = '/@([^\s]+)/'; This pattern says: find instance of '@' then match anything that isn't a space after the @? Tis very clever if that's the case - regex really is quite something. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365126 Share on other sites More sharing options...
Christian F. Posted July 28, 2012 Share Posted July 28, 2012 Yep, that's exactly what it says. With the added bit of storing that "everything that isn't a space" in a sub group, which is then referenced to by the "$1" in the replacement string. RegExp sure is great, but easily abused and can get really complex really fast. So take heed when using it. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365128 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 Yeah, noticed it right after posting. Changed it while you were posting. What about $string = "@sam loves po-ta-toes"; Out of interest so i understand this patttern (as my regex is a bit rusty): $match = '/@([^\s]+)/'; This pattern says: find instance of '@' then match anything that isn't a space after the @? Tis very clever if that's the case - regex really is quite something. You've got it. The + means 1 or more times, and the brackets capture the result to be used later. So, it has to be an @ followed by one or more characters than aren't spaces. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365129 Share on other sites More sharing options...
Christian F. Posted July 28, 2012 Share Posted July 28, 2012 What about $string = "@sam loves po-ta-toes"; Seems we both missed out on something: how would i go about changing it so its looking for " @" instead of "@" this way people can still type email address's and it will ignore it Though, I should indeed have thought about the possibility of the search term being the first word in the string. I shall fix that right away. Addendum: Hmm.... Seems I can't go back and edit my post any more. Oh, well. Here's the updated version: $string = "@Christian is testing!"; $replace = '$1<a href="" class="profile_name" id="$2">$2</a>'; echo preg_replace ("/((?<!\\S))@(\\S+)/u", $replace, $string); Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365134 Share on other sites More sharing options...
xyph Posted July 28, 2012 Share Posted July 28, 2012 (?:^|\s)@(\S+) Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365136 Share on other sites More sharing options...
Christian F. Posted July 28, 2012 Share Posted July 28, 2012 Make that sub group capturing, and we agree. Quote Link to comment https://forums.phpfreaks.com/topic/266392-replacingextracting-an-item-in-a-string/#findComment-1365137 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.