2016-07-01 208 views
1

我無法從PHP中的URL列表中獲取特定文本。 這裏是網址範例檢查字符串是否包含具有數字的特定字符PHP

$arrString = array(
"http://example.expl/text-t350/", 
"http://example.expl/text-t500-another-text/" 
"http://example.expl/text-t20/text-example/" 
); 

我只需要 'T' 字用TE號: T350 T500 T20

我試過如下:

foreach ($arrString as $key => $value) { 
if (strpos($value, "t".filter_var($value, FILTER_SANITIZE_NUMBER_INT)) !== true) { 
    echo "Url with t price ".$value."<br>"; 
} 

} 

但沒有工作;(
希望你能幫助我...

謝謝inadvance!

+1

您需要使用正則表達式。 –

+0

你是什麼意思?你能告訴我一個例子嗎? – Emin

+0

在PHP中使用正則表達式,你可以很容易地從URL分離tnum ...檢查這個鏈接,你會得到的資源... http://www.tutorialspoint.com/php/php_regular_expression.htm –

回答

2

你需要使用正則表達式,見下面的例子:

$arrString = array(
    "http://example.expl/text-t350/", 
    "http://example.expl/text-t500-another-text/", 
    "http://example.expl/text-t20/text-example/" 
); 

foreach ($arrString as $key => $value) { 
    if(preg_match('/text-(t\d+)/', $value, $matches)) { 
     echo $matches[1] . "<br>"; 
    } 
} 

說明:

text-匹配字面上
(捕獲組開始
t匹配字面上
\d匹配一個數字
+ 1或更多
)捕獲組結束

+0

它的工作原理! 非常感謝! – Emin

相關問題