How to get word between particular symbols in string

Solution:1

Try this

$str = 'http://example.abc.com';
$last = explode("/", $str, 3);

$ans = explode('.',$last[2]);
echo $ans[0];

Solution:2

You can use parse_url

<?php
    // Real full current URL, this can be useful for a lot of things
    $url = 'http'.((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    // Or you can put another url
    $url = 'https://www.example.foo.biz/';
    // Get the host name
    $hostName = parse_url($url, PHP_URL_HOST);
    // Get the first part of the host name
    $host = substr($hostName, 0, strpos($hostName, '.'));

    print_r($url);
    print_r($hostName);
    // Here is what you want
    print_r($host);

?>

Solution:3

you can use strpos:

<?php

$url = "http://www.example.com";

/* Use any of you want.
$url = "https://example.com";
$url = "https://www.example.abc.com";
$url = "https://www.www.example.com"; */

if ($found = strpos($url,'example') !== false) {


  echo "it exists";

}
?>

EDIT:

So this is what I cam up with now, using explode and substr:

$url = "http://www.example.com";

/* Use any of you want.
$url = "https://example.com";
$url = "https://www.example.abc.com";
$url = "https://www.www.example.com"; */

 $exp ='example';
if ($found = strpos($url, $exp) !== false) {

echo $str = substr($url, strpos($url, $exp));  
echo "<br>". "it exists" . "<br>";

$finalword = explode(".", $str);
var_dump($finalword);

}
?>