phpregexpreg-matchpreg-match-allmention

preg match to get text after @ symbol and before next space using php


I need help to find out the strings from a text which starts with @ and till the next immediate space by preg_match in php

Ex : I want to get @string from this line as separate. In this example, I need to extract "@string" alone from this line.

Could any body help me to find out the solutions for this.

Thanks in advance!


Solution

  • PHP and Python are not the same in regard to searches. If you've already used a function like strip_tags on your capture, then something like this might work better than the Python example provided in one of the other answers since we can also use look-around assertions.

    <?php
    $string = <<<EOT
    I want to get @string from this line as separate.
    In this example, I need to extract "@string" alone from this line.
    
    @maybe the username is at the front.
    
    Or it could be at the end @whynot, right!
    
    dog@cat.com would be an e-mail address and should not match.        
    
    EOT;
    
    echo $string."<br>";
    
    preg_match_all('~(?<=[\s])@[^\s.,!?]+~',$string,$matches);
    print_r($matches);
    ?>
    

    Output results

    Array
    (
        [0] => Array
            (
                [0] => @string
                [1] => @maybe
                [2] => @whynot
            )
    
    )       
    

    Update

    If you're pulling straight from the HTML stream itself, looking at the Twitter HTML it's formatted like this however:

    <s>@</s><b>UserName</b>
    

    So to match a username from the html stream you would match with the following:

    <?php
    
    $string = <<<EOT
    <s>@</s><b>Nancy</b> what are you on about?
    
    I want to get <s>@</s><b>string</b> from this line as separate. In this example, I need to extract "@string" alone from this line.
    
    <s>@</s><b>maybe</b> the username is at the front.
    
    Or it could be at the end <s>@</s><b>WhyNot</b>, right!
    
    dog@cat.com would be an e-mail address and should not match.        
    
    EOT;
    
    $matchpattern = '~(<s>(@)</s><b\>([^<]+)</b>)~';
    preg_match_all($matchpattern,$string,$matches);
    
    
    $users = array();
    foreach ($matches[0] as $username){
        $cleanUsername = strip_tags($username);
        $users[]=$cleanUsername;
    }
    
    print_r($users);
    

    Output

    Array
    (
        [0] => @Nancy
        [1] => @string
        [2] => @maybe
        [3] => @WhyNot
    )