2016-09-09 96 views
0

我只想問如何找到一些字符串中的文本。我知道我會在PHP中使用preg_match函數,但我不知道如何使用它。假設我想做一個虛擬主機。我將這樣做:如何使用preg_match

<?php 
$web_hosting_service="hostingservice.com"; 
$subdomain_name = "subdomain.hostingservice.com"; 

if(preg_match($web_hosting_service, $subdomain_service) { 
    echo 'true'; 
} else { 
echo 'false'; 
} 

如果不是,我需要做的

+0

你缺少你的正則表達式的分隔符,你也想逃避你的搜索字符串,所以你拿走了特定字符的特殊含義。你也可以使用'strpos()',看看它。 – Rizier123

回答

1

對於簡單的字符串中之弦檢查,使用strstr代替preg_match。它更簡單:

<?php 
$web_hosting_service="hostingservice.com"; 
$subdomain_name = "subdomain.hostingservice.com"; 

if(strstr($subdomain_name, $web_hosting_service)) { 
    echo 'true'; 
}