2016-07-01 207 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

相关问题