SFY
SFY

Reputation: 61

how to get the string between two character in this case?

I want to get the string between "yes""yes"

eg.

yes1231yesyes4567yes

output:

1231,4567

How to do it in php? may be there are 1 more output '' between yesyes right?

Upvotes: 1

Views: 68

Answers (2)

Matthew Watts
Matthew Watts

Reputation: 1

Good Evening,

If you are referring to extracting the digits (as shown in your example) then you can achieve this by referencing the response of Christopher who answered a very similar question on this topic:

PHP code to remove everything but numbers

However, on the other hand, if you are looking to extract all instances of the word "yes" from the string, I would recommend using the str_replace method supplied by PHP.

Here is an example that would get you started;

str_replace("yes", "", "yes I do like cream on my eggs");

I trust that this information is of use.

Upvotes: 0

scrowler
scrowler

Reputation: 24406

In this particular example, you could use preg_match_all() with a regex to capture digits between "yes" and "yes":

preg_match_all("/yes(\d+)yes/", $your_string, $output_array);

print_r($output_array[1]);

// Array
// (
//     [0] => 1231
//     [1] => 4567
// )

And to achieve your desired output:

echo implode(',', $output_array[1]); // 1231,4567

Edit: side reference, if you need a looser match that will simply match all sets of numbers in a string e.g. in your comment 9yes123yes12, use the regex: (\d+) and it will match 9, 123 and 12.

Upvotes: 4

Related Questions