2010-06-30 34 views
3

我有一個字符串,如下列:PHP正則表達式幫助解析字符串

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 

我想有這一段分成基礎上,<s> </s>標籤的數組,所以我有以下數組作爲結果:

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

這是我目前使用正則表達式:

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

但這個只返回一個包含<s> </s>標籤之間的文本的數組,它忽略了在這些標籤之前和之後發現的文本。 (在上面的例子中,將只返回數組元素1和2

任何想法

回答

2

我可以用preg_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)); 

,並得到這樣的輸出:

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 
) 

除了產生一個額外的數組元素(索引2)那裏的所述片段之間[city][State].</s>換行符和<s>Josh's real estate company

雖然添加一些代碼來刪除空白匹配將是微不足道的,但我不確定是否需要這樣做。

+0

額外的數組元素是好的,但它似乎在尋找的只是'',這意味着像'我的名字是鮑勃。 im 17。'和'我的名字是鮑勃。 im 17'會被分成2個元素,它是否可以改變,所以第一個例子只保存在1個數組元素中? (我希望未打開的''不匹配)。 – 2010-06-30 06:07:46

+0

此外,如果可以刪除空的數組元素,那麼我更喜歡它。 – 2010-06-30 06:24:10

+0

我會用我的代碼撥弄一下,然後更新我的答案,如果我只能匹配正確打開和關閉的標籤,並刪除空的元素。 – BoltClock 2010-06-30 06:27:22