preg_replace entire string if currently contains a string - php

i would like to replace the entire URL to completely new one if the URL currently contains an unwanted string.
this is what i tried.
if (preg_match('/(site1.com)/i')){
$url = preg_replace('/(site1.com)/i',('http://www.site2.com/newsite/index.php?a=b'),$url);
}
any help is appreciated.

It doesn't seem necessary to do a preg_replace if you want to replace the entire string. So, try this:
$url = 'http://www.site1.com/';
$pattern = '/(site1.com)/i';
$newUrl = 'http://www.site2.com/newsite/index.php?a=b';
if (preg_match($pattern, $url)){
$url = $newUrl;
}
print_r($url);

Related

Remove only 3 characters after a specific string

Hi I have this string for example
http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/
I want to remove only 3 characters after bbbb-bbbbbbbbbb-2/, so basically I want to remove the it/ part (This it/ part may not always be it but it can be es/ or en/ or different languages always 2 characters )
The following will work provided the the URL structure doesn't change. I assume you're wanting to remove the language part of the URL.
<?php
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$parsedURL = parse_url($url);
$path = explode('/', $parsedURL['path']);
unset($path[2]);
$url = "{$parsedURL['scheme']}://{$parsedURL['host']}";
$url .= implode('/', $path);
var_dump($url);
// string(47) "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/"
You can use regex to selecting target part of string and run it in preg_replace().
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
echo preg_replace("#(.*)\w{2}/([^/]+/)$#", "$1$2", $url);
See result of code in demo
Define your languages in an array. If a language is not defined in the array URL will be the same.
$mLanguages = ["en","it","bg","gr"];
$mURL = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$mURL = removeLanguageFromURL($mURL, $mLanguages);
echo $mURL; // Output http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/
function removeLanguageFromUrl($mURL, $mLanguages){
foreach($mLanguages as $language){ // Search languages
if(strpos($mURL, $language) !== false) // If language is found in url remove it
$mURL = str_replace('/' . $language,'', $mURL);
}
return $mURL;
}

How to remove two same and random characters following each other in string - PHP

Need to remove random aa/ or bb/ to zz/ letters (with slash) to get /logo/picture.png
$url = "/logo/aa/picture.png";
$url = "/logo/bb/picture.png";
$url = "/logo/cc/picture.png";
This is an alternative which doesn't care what's contained in that url part or what lengths url parts have:
$urlParts = explode('/', $url);
array_splice($urlParts, count($urlParts) - 2, 1);
$url = implode('/', $urlParts);
If the $url is always of the form you provided you could do:
$str1 = substr($url,0,5);
$str2 = substr($url,8,strlen($url));
$url = $str1.$str2;
if it's not always of the same form you could determine the substrings indexes programmatically, maybe using strpos function. More detail here
Luca Angioloni solution is correct but this is more stable:
$url preg_replace("/\/[a-z]{2}\//", "/", $url);
This will work even on url like: /img/xz/picture.png but if you have an url like this /ig/aa/picture.png this will remove /ig and not /aa

how change the string in php?

eg:
$url=http://www.example.com/.
how to make the $url to this style.
http://test.google.com/example.com in php?
PHP: Simple and easy way to format URL string should clear everything up for you
$url_parts=parse_url($url);
echo $url="http://test.google.com/".str_replace('www.','',$url_parts['host']);
$url = "http://www.example.com";
$Step1 = str_replace(array("http://", "https://", "www."), "", $url);
$Step2 = explode("/", $Step1);
$newUrl = "http://test.google.com/".$Step2[0];
Basically what I did is replacing any http://, https:// and www. strings from the URL in $url and replace them with a blank string. Then I explode the result of this replace on an '/' character, because there might be an URL given as http://www.test.com/mydir/ so we lose the mydir. If this isn't want you need, skip step 2 and replace $Step2[0] with $Step1 on the last line.
This last line adds the URL you want in $newUrl
Try this:
$url = "http://www.example.com/";
$url = preg_replace("/(?:http:\/\/)?(?:www\.)?([a-z\d-\.]+)\/.*/", "http://test.google.com/$1", $url);

Replace string using php preg_replace

Hi all i know preg_replace can be used for formatting string but i need help in that concerned area my url will be like this
http://www.example.com/index.php/
also remove the http,https,ftp....sites also
what i want is to get
result as
example.com/index.php
echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);
echo implode("",$parts);
EDIT
To use http_build_url you needs pecl_http you can use implode as alternate
Something like this
$url = "http://www.example.com/index.php";
$parts = parse_url($url);
unset($parts['scheme']);
echo preg_replace('/^((ww)[a-z\d][\x2E])/i', '', join('', $parts));
Output
example.com/index.php
Example #2
$url = "http://ww3.nysif.com/Workers_Compensation.aspx";
Output
nysif.com/Workers_Compensation.aspx

PHP preg_replace Help

I need help replacing a link like google.com into http://www.google.com
$url = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$url);
$output = htmlspecialchars(urldecode($url));
I'm using an iframe like:
<iframe src='$url'></iframe>
However, if the src="google.com" instead of "http://google.com" it will not work. So, how can I transform google.com into http://www.google.com?
Here's a non regex hack way to do it.
$url = 'google.com';
function addHTTP($url) {
return 'http://'.str_replace('http://','',$url);
}
$url = "www.google.com";
if(!preg_match("/^https/i",$url))
$url = "http://$url";
There are better ways to do this, but this will work:
if(!preg_match("#^http:\/\/#", $url)) {
$url = "http://".$url;
}
$url = 'http://' . $url;
the simpliest way possible :o
How about checking if http:// is on the beginning of it and if not tag it on? Like so:
$url = 'google.com';
if (!preg_match('#^http://#', $url)) {
$url = 'http://'.$url;
}
Just for fun, here's one that uses just preg_replace by taking advantage of a negative lookahead. However, I agree with the other solutions here, that it is probably best to just to a preg_match and then a string concatenation.
$url = preg_replace('#^(?!https?://)#', 'http://', $url);
If you just want to make your RegEx match google.com e.a., all you have to do is make www. optional. Please note that this may introduce other problems, such as end.begin being recognized as an URL.
/([^\w\/])((www\.)?[a-z0-9\-]+\.[a-z0-9\-]+)/i

Categories