PHP Regex Help for string parsing

I have a line such as:

Are you looking for a quality real estate company? <s>Josh real estate firm specializes in helping people find homes from [city][State].</s> <s>Josh real estate company is a boutique real estate firm serving clients locally.</s> In [city][state] I am sure you know how difficult it is to find a great home, but we work closely with you to give you exactly what you need 

I would like this paragraph to break into an array based on <s> </s> tags, so as a result I got the following array:

 [0] Are you looking for a quality real estate company? [1] Josh real estate firm specializes in helping people find homes from [city][State]. [2] Josh real estate company is a boutique real estate firm serving clients locally. [3] In [city][state] I am sure you know how difficult it is to find a great home, but we work closely with you to give you exactly what you need 

This is the regular expression that I am currently using:

 $matches = array(); preg_match_all(":<s>(.*?)</s>:is", $string, $matches); $result = $matches[1]; print_r($result); 

But this one only returns an array containing the text found between the <s> </s> tags, it ignores the text found before and after these tags. (In the example above, it will only return elements of array 1 and 2.

Any ideas?

+3
source share
2 answers

Closest I could use instead of preg_split() :

 $string = <<< STR Are you looking for a quality real estate company? <s>Josh real estate firm specializes in helping people find homes from [city][State].</s> <s>Josh real estate company is a boutique real estate firm serving clients locally.</s> In [city][state] I am sure you know how difficult it is to find a great home, but we work closely with you to give you exactly what you need STR; print_r(preg_split(':</?s>:is', $string)); 

And I got this conclusion:

 Array ( [0] => Are you looking for a quality real estate company? [1] => Josh real estate firm specializes in helping people find homes from [city][State]. [2] => [3] => Josh real estate company is a boutique real estate firm serving clients locally. [4] => In [city][state] I am sure you know how difficult it is to find a great home, but we work closely with you to give you exactly what you need ) 

In addition, an additional array element (index 2 ) is created, where there is a new line between the [city][State].</s> and <s>Josh real estate company fragments of the <s>Josh real estate company .

It would be trivial to add some code to remove matches, but I'm not sure if you want to.

+2
source

I suggest you look into the DOM http://php.net/manual/en/book.dom.php

+1
source

Source: https://habr.com/ru/post/1305154/


All Articles