The Real Hero
The Real Hero

Reputation: 47

Getting specific parts from a string to another

I have a string variable like following

$str= '[[["how ","no use","",""],["to ","no use","",""],["solve this","no use","",""]]]';

And i want some parts of this string to make another one like following

$result="how to solve this";

Is it possible to create $result from $str?

Am new to php someone help me to solve this or any idea to solve this.

Upvotes: 0

Views: 38

Answers (2)

Shadow Radiance
Shadow Radiance

Reputation: 1369

Given the structure of your string, you could probably use preg_grep to find any substring matching [" until the next "

$regex = '/\\["([^"])"/';
$matches = preg_grep($regex,$str);
$result = implode('',$matches);

Note... my regex might be rusty ;-)

Upvotes: 0

castis
castis

Reputation: 8223

since the string is json, you can use json_decode to turn that string into an array. Since the whole thing is wrapped in [], reset is used to shear off the out outer array.

$string= '[[["how ","no use","",""],["to ","no use","",""],["solve this","no use","",""]]]';

$result = '';
foreach(reset(json_decode($string)) as $piece)
{
    $result .= reset($piece);
}

echo $result;

Upvotes: 2

Related Questions