preg_replace() doesn't work as expected - php

I want to change this url from:
https://lh3.googleusercontent.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif
to this:
http://3.bp.blogspot.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif
This is my code, but it's not working as expected:
$link = preg_replace('#^https?://.*?/(.+?/)(s\d+/)?([\w_-]+\.[\w]{3,})?$#i','http://3.bp.blogspot.com/$1s0/$3',$url);

It's a simple string replace.
Search for "https://lh3.googleusercontent.com/". Replace with "http://3.bp.blogspot.com/".
str_replace() will do this. Am I missing something?

$s = "https://lh3.googleusercontent.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif";
$path = parse_url($s);
echo 'http://3.bp.blogspot.com' . $path['path'];
UPDATE You can don't receive all the parts of an url, but take only the needed part
echo 'http://3.bp.blogspot.com' . parse_url($s, PHP_URL_PATH);

Related

Preg replace php delete after character

I have a URL like this : www.test.fr/dir/file.html#hello I would delete everything after the character #. I have try this /#[a-z0-9]+/
You code is almost correct, this works just fine:
$new = preg_replace('/#[a-z0-9]+/', '', 'www.test.fr/dir/file.html#hello');
print ($new);
prints:
www.test.fr/dir/file.html
You can test it here
You can explode by '#' and get the first position. Something like this:
$url = "www.test.fr/dir/file.html#hello";
$result = explode("#",$url)[0];

how to get filename based on the number of characters after the dot

hello here is what I have :
filename mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw
using php how do I capture the name of the file which would be anything.ext and get rid of the rest.
so return : mycoolfile.zip
I know it can be accomplished using regular expressions but I am not familiar with those, I am open to any suggestions or any way to accomplishing the task as long as it works for anyfilename.ext?blablablaafter
it doesnt matter if its regex or not as long as it works. any help please
$path = '/lul/what/mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$file = basename(preg_replace('/\?.*$/', '', $path));
Use parse_url to get the name
<?php
$str = "mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1";
$parsed_url = parse_url($str);
echo $parsed_url["path"] . "<br>";
You can make use of parse_url function:
$u = 'filename mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$arr = parse_url($u);
echo $arr['path']; //=> OUTPUT: filename mycoolfile.zip
Could you use '?' as a deliminator in explode() and use the value from an array, or perhaps use preg_split()?
edit: or use the much better solutions above! :-)
You may also try this
$u = 'mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$url=explode("?",$u);
echo $url['0'];

String at the end of a text inside another string

I would like to use one string inside another string, like this:
$url = "http://www.sample.com/index.php?nr=$string[0]";
how should I insert this string at the end?
$url = "http://www.sample.com/index.php?nr={$string[0]}";
or
$url = "http://www.sample.com/index.php?nr=" . $string[0];
just like your example
$url = "http://www.sample.com/index.php?nr=$string[0]";
As a solution to your problem please try executing following code snippet.
You need to concatenate strings in php using dot operator(.)
$url = "http://www.sample.com/index.php?nr=".$string[0];

Function to shorten a specific string

I have this string:
$str="http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
Is there a built-in php function that can shorten it by removing the ._SL110_.jpg part, so that the result will be:
http://ecx.images-amazon.com/images/I/418lsVTc0aL
no, there's not any built in URL shortener php function, if you want to do something similar you can use the substring or create a function that generates a short link and stores the long and short value somewhere in database and display only the short one.
well, it depends if you need a regexp replace (if you don't know the complete value) or if you can do a simple str_replace like below:
$str = str_replace(".SL110.jpg", "", "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg");
You can use preg_replace().
For example preg_replace("/\.[^\.]+\.jpg$/i", "", $str);
I would recommend using:
$tmp = explode("._", $str);
and then using $tmp[0] for your purpose, if you make sure the part you want to get rid of is always separated by "._" (dot-underscore) symbols.
You can try
$str = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
echo "<pre>";
A.
echo strrev(explode(".", strrev($str), 3)[2]) , PHP_EOL;
B.
echo pathinfo($str,PATHINFO_DIRNAME) . PATH_SEPARATOR . strstr(pathinfo($str,PATHINFO_FILENAME),".",true), PHP_EOL;
C.
echo preg_replace(sprintf("/.[^.]+\.%s$/i", pathinfo($str, PATHINFO_EXTENSION)), null, $str), PHP_EOL;
Output
http://ecx.images-amazon.com/images/I/418lsVTc0aL
See Demo
you could do this substr($data,0,strpos($data,"._")), if what you want is to strip everything after "._"
No, it is not (at least not directly). Such URL shorteners usually generate unique ID and remember your original URL and generated ID. When you enter such url, you start a script, which looks for given ID and then redirect to target URL.
If you want just cut of some portion of your string, then assuming that filename format is as you shown, just look for 1st dot and substr() to that place. Or
$tmp = explode('.', $filename);
$shortName = $tmp[0];
If suffix ._SL110_.jpg is always there, then simply str_replace('._SL110_.jpg', '', $filename) could work.
EDIT
Above was example for filename only. Whole code would be:
$url = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
$urlTmp = explode('/', $url);
$fileNameTmp = explode( '.', $urlTmp[ count($urlTmp)-1 ] );
$urlTmp[ count($urlTmp)-1 ] = $fileNameTmp[0];
$newUrl = implode('/', $urlTmp );
printf("Old: %s\nNew: %s\n", $url, $newUrl);
gives:
Old: http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg
New: http://ecx.images-amazon.com/images/I/418lsVTc0aL

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

Categories