user2417624 user2417624 - 1 year ago 64
PHP Question

Extracting part of string located after second occurrence of specific character and second occurrence of another character

How can extract peace of string which is located between second occurrence of the ":" character and second occurrence of "," in peace of string?

substr() can't help me, since the length of the peace of the string that I want to extract may vary. I am not posting any code, since I have no clue where to start from. I am searching string manipulation functions, but without any luck so far. I am not sure if this can be done with regular expressions either, since it is second concurrence.

Here is the part of the string that I am trying to manipulate

[3] => 93,text:Πού είναι αυτή η πόλη:,photo:c083,correct:2,answers:[{text:Βενεζουέλα.,correct:false},{text:Βραζιλία.,correct:false},{text:Πέρυ.,correct:true}]},{

Answer Source

Id try something like the following assuming you're always trying to get the photo value:

$str = "93,text:Πού είναι αυτή η πόλη:,photo:c083,correct:2,answers:[{text:Βενεζουέλα.,correct:false},{text:Βραζιλία.,correct:false},{text:Πέρυ.,correct:true}]},{";
preg_match_all('/photo:[\w+]*/', $str, $match);
$photo = explode(':', $match[0][0]);
$photo_id = $photo[1];
echo $photo_id;
// > c083

Hope that helps.