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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.