Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Get substring of a complex string in php

I have the following string:

"@String RT @GetThisOne: Natio"

How can I get "GetThisOne" from this string?

Upvotes: 0

Views: 200

Answers (4)

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41885

You can use preg_match like this:

<?php

$string = "@String RT @GetThisOne: Natio";
preg_match('/@.*@([A-Za-z]*)/', $string, $matches);
echo $matches[1]; // outputs GetThisOne

Here, the pattern is the folowing: find an numeric string after the second @. Ideone example.

Upvotes: 2

Robbo_UK
Robbo_UK

Reputation: 12149

You could always try php explode function

$string = "@String RT @GetThisOne: Natio"

$arr = explode('@', $string);

if(is_array($arr) && count($arr)>0)
{
   echo $arr[0]."\n";
   echo $arr[1];
}

will echo out

String RT

GetThisOne: Natio

Upvotes: 1

Hamikzo
Hamikzo

Reputation: 254

Find "@" position, and calculate position of ":" after the "@" found.

$at = strpos($string,'@');

substr($string,$at,strpos($string,':',$at)-$at);

Upvotes: 1

Tom
Tom

Reputation: 4592

You can use the substr function.

Upvotes: 1

Related Questions