[英]check if string contains specific character with number PHP
我在從PHP網址列表中獲取特定文本時遇到麻煩。 這是網址的示例
$arrString = array(
"http://example.expl/text-t350/",
"http://example.expl/text-t500-another-text/"
"http://example.expl/text-t20/text-example/"
);
我只需要帶有te編號的't'字符即可: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>";
}
}
但是沒有用;(
希望你能幫我...
提前致謝 !
您需要使用正則表達式,請參見下面的示例:
$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或更多
)
捕獲組結束
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.