[英]How to find a substring from a string using regular expression in php?
我有一個類似“我愛我的國家”的字符串。 如果我搜索“國家”,我的程序將顯示“存在”。 但是,如果我搜索“計數”,我的程序將顯示“不存在”。
這是您要找的東西嗎?
我不是Regex的主人,所以有人可能會提供更理想的解決方案,但是僅搜索帶有單詞邊界的國家應該可以為您提供幫助。
/\\b(country)\\b/g
因此,在PHP中,如下所示:
$string = 'I love my country';
$matchExists = preg_match('/\b(country)\b/', $string);
echo ($matchExists) ? 'Match Found' : 'No Match Found'; // Output: Match Found
失敗的是:
$string = 'I love mycountry';
$matchExists = preg_match('/\b(country)\b/', $string);
echo ($matchExists) ? 'Match Found' : 'No Match Found'; // Output: No Match Found
提醒您,您可以在此處看到許多不同的輸出,以及測試您可能擁有的任何其他輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.