Abhik
Abhik

Reputation: 674

How To Get The Value After A Certain Character in PHP and RegEX?

How do I get the value after a certain character ( '@' in my case) in a string? I have this code, but its not working; There will be only alphanumeric characters and/or space.

$data = 'Someone @Will Be There';

preg_match ("/@([a-zA-Z0-9 ]*)/", $data, $match);
return $match [0];

All I want to get that "Will Be There" if that @ is present in the string.
Thanks

Edit: Extremely sorry for my mistake. I don't wanna remove that '@', I need to get What are After that. Sorry again, people.

Upvotes: 0

Views: 1130

Answers (7)

Ramesh Moorthy
Ramesh Moorthy

Reputation: 689

Better can use simple string replacement method and try this,

$data = 'Someone @Will Be There';
$result = str_replace('@','',$data); // result is: Someone Will Be There

Don't Use this:

//preg_match ("/@([a-zA-Z0-9]*)/", $data, $match);
//return $match [0];

Upvotes: 0

keyboardSmasher
keyboardSmasher

Reputation: 2811

Changed my code to reflect your changes...

$data = 'Someone @Will Be There';
$character = '@';

if ($string = stringAfterCharacter($data, $character)){
    echo $string;
}else{
    echo "Character: '$character' not found.";
}


function stringAfterCharacter($data, $character='@'){
    if (($pos = strpos($data, $character)) !== false){
        return substr($data, $pos + 1);
    }else{
        return false;
    }
}

Upvotes: 1

Toto
Toto

Reputation: 91385

The first group is in $match[1], not in $match[0]

Change your code to:

$data = 'Someone @Will Be There';
preg_match ("/@([a-zA-Z0-9 ]*)/", $data, $match);
return $match[1];

Upvotes: 0

Rajat Garg
Rajat Garg

Reputation: 355

You question seems ambiguous, If you want to remove '@' from the string, then you can use the answers with str_replace. If you want the value after '@' you should check for $match[1] in your own code.

Upvotes: 0

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

This will remove any symbols from the string

$data = 'Someone @Will Be There';
echo preg_replace("/([^A-Za-z0-9 ]*)/", '', $data); //Add character in the pattern if not to be filtered

Output:

Someone Will Be There

Upvotes: 0

sn00k4h
sn00k4h

Reputation: 443

You don't need to use regex, which generally is slow, to do that. Just do: return str_replace('@', '', $data);

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

You can use the function strpos() for that purpose. It searches for a substring - or at least a char - in a string and the returns the first position of that substring.

$position = strpos('I am @ the penthouse', '@');

When the substring was not found in the string the method returns false.

There are common pitfalls that come with strpos() in php. Check the following example :

if(!strpos('@stackoverflow', '@')) {
  echo 'the string contains no @';
 }

The would output that '@' was not found although the string contains an at. Thats because of the weak data typing in PHP. The previous strpos() call will return int(0) because it is the first char in string. But this will make the if fail unless you do :

if(strpos('@stackoverflow', '@') === FALSE) {
   echo 'the string contains no @';
}

Upvotes: 0

Related Questions