-1

Did you know if it's possible to catch exact string from uri with a regex ?

For exemple :

https://example.com
https://example.com/pizza

These two URIs are in the same text but I have to make a preg_replace for https://example.com only.

str_replace does not work here because it necessarily affects the second URI :P

Thanks for your time !

Edit : It's work with this expression : https://regex101.com/r/K7rgTh/6 But I need to catch inside html tags or not :P

3 Answers3

-1

You can use a fourth parameter like this:

preg_replace($search, $replace, $subject, 1);

This problem was solved on this posting Replace only first match using preg_replace.

Tarsis Cruz
  • 95
  • 1
  • 7
-1

Use RegEx https:\/\/example\.com(?=\s|$), it will replace only https://example.com

Regex Explanation and Demo : https://regex101.com/r/K7rgTh/4 Usage:

preg_replace("/https:\/\/example\.com(?=\s|$)/i", "replacement", $text)

I think this will solve the problem

Illya
  • 1,268
  • 1
  • 5
  • 16
-1

you can get your domain with http or https like this :



        if(isset($_SERVER['HTTPS'])){
            $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
        }
        else{
            $protocol = 'http';
        }
        $url = $protocol . "://" . $_SERVER['HTTP_HOST'];