Get value from "value" attribute declarations in a string [duplicate]
Asked Answered
A

5

12

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=".

Antonio answered 8/7, 2015 at 9:52 Comment(0)
W
13

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

Widthwise answered 8/7, 2015 at 10:20 Comment(3)
Hey mohammad thnk u for ur ans.. It works great for my que but i edited my que just now. Came to know tht value also contains some letters like hex code..Antonio
Thnk uuu very much mohammad... It works.. First it didnt becasue there was a space bw "value=" and value lol... So i added space to ur code too and it worrrkkss... Sorry.. I m so happy thnk u very much.. By the way wht does #value=[^\s]+)# means ? Particularly tht part after =Antonio
[^\s]+ is \S+Egad
L
8

Do it via regular expression

$str = 'b2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134';

preg_match_all("/value=([^\s]+)/", $str, $matches);

echo implode(" ", $matches[1]);

Here you can see demo

Larry answered 8/7, 2015 at 9:55 Comment(6)
Thnk u very much hassan for quick reply but i ll need space or something to divide 2 values too. Or better if each value comes on new line.Antonio
@Antonio I have updated my answer please try it or see the demo.Larry
I see it but it also shows numbers which r before value=Antonio
Edited my que. Sorry value also contains some letters it is like hexAntonio
@Hassan this code is not complete. it also contains other numbers. I Wrote the answer.Widthwise
[^\s]+ is \S+Egad
M
1

You could use a lookahead to find all value= and take all characters after that until a space character is encountered then implode the results using a space.

$string = 'Ab2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134';

preg_match_all("/(?=value\=([^\s]+))/", $string, $matches);

$result = implode(" ", $matches[1]);

The output is

284t810 39h1047 3700p134
Milton answered 8/7, 2015 at 10:30 Comment(1)
[^\s]+ is \S+Egad
U
1

Sticking to substr() and strpos() you can do the following as long as you can trust the format of the data.

$s = 'Ab2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134';
echo "Input string: $s<br>\n";

$out = '';
$offset = 0;
while ( $offset = strpos($s,'=',$offset) )
{
  $end = strpos($s,' ',$offset);
  if ( $end )
    $out .= substr($s,$offset+1,$end-$offset);
  else
    $out .= substr($s,$offset+1);

  $offset++;
}

echo "Output string: $out<br>\n";

This will yield the following:

Input string: Ab2cds value=284t810 shfn4wksn value=39h1047 hs2krj8dne value=3700p134
Output string: 284t810 39h1047 3700p134

I'm guessing that perhaps you wanted to use a strpos() based solution for efficiency purposes.

Uphill answered 8/7, 2015 at 10:59 Comment(0)
F
0

Try this

$data = 'Ab2cds value=284810 shfn4wksn value=391047 hs2krj8dne value=3700134';
$array_data = explode(' ', $data);
foreach ($array_data as $array_dat) {
    $data_list[] = strstr($array_dat, '=');
}
foreach ($data_list as $key => $value) {
    $array[$key] = str_replace('=', '', $value);
}
var_dump(array_filter($array));

OUTPUT

array(3) { [1]=> string(6) "284810" [3]=> string(6) "391047" [5]=> string(7) "3700134" } 
Feoffee answered 8/7, 2015 at 10:34 Comment(1)
The output he wants is: 284t810 39h1047 3700p134. This is removing the letters.Horsewoman

© 2022 - 2024 — McMap. All rights reserved.