Doruk Ayar Doruk Ayar - 2 months ago 7
PHP Question

How Can I use preg_match

I just want to ask how to find some text in a string. I know that I will gonna use

preg_match
function in PHP but I don't know how to use it. Let's say I want to make a web hosting. Shall I do like this:

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

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


If not, what I need to do

Answer

For simple string-within-a-string checking, use strstr instead of preg_match. It's much simpler:

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

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