sebasparola
sebasparola

Reputation: 200

Catch a word into quotes using preg_replace

I have a string with many caracters and I need obtain data from it. First of all, I did explode by ';', now I have an array and of each row I have a word into quotes.

I want to remove all, less this word into quotes. I know that is more easy to obtain these words with preg_match, but how is into an array, to save up to go over the array again, I would like to clean it directly with preg_replace.

$array = explode(';', $string);

//36 => string 's:7:"trans_1"' (length=13)
//37 => string 's:3:"104"' (length=9)
//38 => string 's:5:"addup"' (length=11)
//39 => string 's:1:"0"' (length=7)

$array = preg_replace('!   !i', '', $array);

I would like to obtain:

//36 => string 'trans_1' (length=6)
//37 => string '104' (length=3)
//38 => string 'addup' (length=5)
//39 => string '0' (length=1)

I tryed differents things, but I can't rid off the letters outside the quotes.

Upvotes: 0

Views: 172

Answers (2)

chrislondon
chrislondon

Reputation: 12031

While this isn't a direct answer to your question it solves your problem. The data you are looking at came from the php function serialize() to retrieve the data from that string you need to use the php function unserialize().

$data = unserialize($string);

Upvotes: 2

Anirudha
Anirudha

Reputation: 32797

You could try

preg_replace('!.*"([^"]*)".*!i', '\1', $array);

\1 refers to the first captured group!

Upvotes: 2

Related Questions