phptext-parsing

Get value from "value" attribute declarations in a string


Value also contains some letters

I have search through so many questions but I couldn't find it.
I have string like this:

Ab2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134

What I want to do is get all values only in output like:

284t810 39h1047 3700p134

I used substr and strpos combine to get value but it only removes portion of data ahead of first "value=" so output is like:

284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134

I just want to delete everything else and keep only numbered value which is after "value=".


Solution

  • use this code: with this code you get any strings that they are after value=. I think this is the easiest solution.

    $str = 'b2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134';
    preg_match_all('#value=([^\s]+)#', $str, $matches);
    
    echo implode(' ', $matches[1]);
    

    @Kesh : \s means space. [^\s] means everything except space. and + means at least one char. and the () around it is for selecting the string so we can use it after the operation. ([^\s]+) means select everything except space and put them to the $matches