Jump to content

[SOLVED] @ Parsing


jaikob

Recommended Posts

I'm trying to program @ notation, like twitter does. For now I'm just literally learning about regex, and have no clue on what do do to achieve what I want.

 

I am using preg_match for now until I get my regex right.

 

How can I parse a string and pull out all of the @username's. Ex:

 

preg_match('regex crap', "Hello @jaikob How are you?", $matches);

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/176434-solved-parsing/
Share on other sites

Yup, And just incase. if you need help on how to use & display this code:

 

<?php
$string="@welcome @decide @homicide @loser  doit andlaugh";
preg_match_all("~@[a-zA-Z0-9]+~", $string, $matches);
foreach ($matches as $val) {

for ($i=0;$i<count($val); $i++)
{
    echo "matched: " . $val[$i] . "\n";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930013
Share on other sites

Hmm.. using a for loop within a foreach like that? Kind of strange. You can simply tap into $matches[0] instead. Also note that you can simplify the pattern by using the 'i' modifier:

 

$string="@welcome @decide @homicide @loser  doit andlaugh";
preg_match_all('#@[a-z0-9]+#i', $string, $matches);
foreach ($matches[0] as $val) {
   echo "matched: $val<br />\n";
}

Link to comment
https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930017
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.