Get value from string php

Edited my que.. 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=39h2047 hs2krj8dne value=3700p134

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

284t810 39h2047 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=39h2047 hs2krj8dne value=3700p134

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

Sorry if there is any confusion. Using stackoverflow for first time.

asked Jul 8, 2015 at 9:52

KeshKesh

2531 gold badge3 silver badges14 bronze badges

0

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=39h2047 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

answered Jul 8, 2015 at 10:20

2

Do it via regular expression

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

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

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

Here you can see demo

answered Jul 8, 2015 at 9:55

HassaanHassaan

6,8485 gold badges29 silver badges48 bronze badges

5

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=39h2047 hs2krj8dne value=3700p134';

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

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

The output is

284t810 39h2047 3700p134

answered Jul 8, 2015 at 10:30

David BarkerDavid Barker

14.3k3 gold badges45 silver badges76 bronze badges

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=39h2047 hs2krj8dne value=3700p134';
echo "Input string: $s
\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
\n";

This will yield the following:

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

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

answered Jul 8, 2015 at 10:59

A SmithA Smith

6211 gold badge4 silver badges10 bronze badges

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" } 

step

2,0292 gold badges23 silver badges41 bronze badges

answered Jul 8, 2015 at 10:34

Arun KumarArun Kumar

1,5771 gold badge17 silver badges32 bronze badges

1

How do you extract a string from a string PHP?

Use the PHP substr[] function to extract a substring from a string. Use the negative offset to extract a substring from the end of the string. The last character in the input string has an index of -1 . Use the negative length to omit a length number of characters in the returned substring.

What is Strval in PHP?

The strval[] function is an inbuilt function in PHP and is used to convert any scalar value [string, integer, or double] to a string. We cannot use strval[] on arrays or on object, if applied then this function only returns the type name of the value being converted. Syntax: strval[ $variable ]

How do I cut a string after a specific character in PHP?

The substr[] and strpos[] function is used to remove portion of string after certain character. strpos[] function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

What is Parse_str in PHP?

Definition and Usage. The parse_str[] function parses a query string into variables. Note: If the array parameter is not set, variables set by this function will overwrite existing variables of the same name.

Chủ Đề