Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于解析字符串的PHP正则表达式帮助_Php_Regex_String - Fatal编程技术网

用于解析字符串的PHP正则表达式帮助

用于解析字符串的PHP正则表达式帮助,php,regex,string,Php,Regex,String,我有一个字符串,如下所示: Are you looking for a quality real estate company? <s>Josh's real estate firm specializes in helping people find homes from [city][State].</s> <s>Josh's real estate company is a boutique real estate firm s

我有一个字符串,如下所示:

Are you looking for a quality real estate company? 

<s>Josh's real estate firm specializes in helping people find homes from          
[city][State].</s>

<s>Josh's 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
这是我当前使用的正则表达式:

$matches = array();
preg_match_all(":<s>(.*?)</s>:is", $string, $matches);
$result = $matches[1];
print_r($result);
$matches=array();
preg_match_all(“:(*?):is“,$string,$matches);
$result=$matches[1];
打印(结果);
但是这个函数只返回一个数组,其中包含在
标记之间找到的文本,它忽略了在这些标记之前和之后找到的文本。(在上面的示例中,它只返回数组元素1和2


有什么想法吗?

我能找到的最接近的方法是使用
preg\u split()

$string = <<< STR
Are you looking for a quality real estate company? <s>Josh's real estate firm 
specializes in helping people find homes from [city][State].</s>
<s>Josh's 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));
除了生成一个额外的数组元素(索引
2
),其中片段
[城市][州]。
Josh的房地产公司
之间有一条新线


虽然添加一些代码来删除空白匹配项很简单,但我不确定您是否希望这样做。

我建议您研究DOM

额外的数组元素很好,但它似乎只查找
,这意味着类似
的东西我的名字是bob.im 17。
我的名字是bob.im 17
都将被拆分为2个元素,是否可以更改它,使第一个示例仅保留在1个数组元素中?(我希望未打开的
不匹配)。另外,如果可以删除空数组元素,那么我更喜欢它。我将稍微修改一下代码,然后更新我的答案,如果我能够匹配正确打开和关闭的标记,并删除空元素。
$string = <<< STR
Are you looking for a quality real estate company? <s>Josh's real estate firm 
specializes in helping people find homes from [city][State].</s>
<s>Josh's 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));
Array
(
    [0] => Are you looking for a quality real estate company? 
    [1] => Josh's real estate firm 
specializes in helping people find homes from [city][State].
    [2] => 

    [3] => Josh's 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
)