Preg replace php delete after character - php

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];

Related

preg_replace() doesn't work as expected

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);

How to remove part of the string from one character to another in php

I have a URL string like
http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9
I want to remove my_link_id:1 from this string. I know I can use any string replace function like
$goodUrl = str_replace('my_link_id:1', '', $badUrl);
But the problem is, the integer part is dynamic. I mean in my_link_id:1 the 1 is dynamic. It is the ID of the link and it can be from 0 to any number. So I want to remove my_link_id:along with any dynamic number from the string.
What I think I should remove part of the string from last / to #. But how can I do that?
you can use regular expressions:
$goodUrl = preg_replace('/(my_link_id:[0-9]+)/ig', '', $badUrl);
You can use preg_replace() php function
http://in1.php.net/preg_replace
CODE:
<?php $string = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
echo $result = preg_replace('/(my_link_id:[0-9]+)/si', '', $string);
?>
Output :
http://mydomain.com/status/statusPages/state/stack:3/#state-9
May following help you
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(my_link_id:[0-9]+)/i', '', $strUrl);
echo $finalUrl;
and If you want remove also last / and # you follow this code
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(\/my_link_id:[0-9]+#)/i', '', $strUrl);
echo $finalUrl;

Remove string starting with something

How can I do the following with php?
This is my example:
http://www.example.com/index.php?&xx=okok&yy=no&bb=525252
I want remove this part: &yy=no&bb=525252
I just want this result:
http://www.example.com/index.php?&xx=okok
I tried this :
$str = 'bla_string_bla_bla_bla';
echo preg_replace('/bla_/', '', $str, 1); ;
but this not what I want.
Going for preg_replace was a good start. But you need to learn about regexes.
This will work:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
echo preg_replace ('/&yy.+$/', '', $str);
Here the regex is &yy.+$
Let's see how this works:
&yy matches &yy obviously
.+ matches everything ...
$ ... until the end of the string.
So here, my replacement says : Replace whatever begins by &yy until the end of the string by nothing, which is actually simply deleting this part.
You can do this:
$a = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$b = substr($a,0,strpos($a,'&yy')); // Set in '&yy' the string to identify the beginning of the string to remove
echo $b; // Will print http://www.example.com/index.php?&xx=okok
Are you always expecting the end part to have the 'yy' variable name? You could try this:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$ex = explode('&yy=', $str, 2);
$firstPart = $ex[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

PHP - strip URL to get tag name

I need to strip a URL using PHP to add a class to a link if it matches.
The URL would look like this:
http://domain.com/tag/tagname/
How can I strip the URL so I'm only left with "tagname"?
So basically it takes out the final "/" and the start "http://domain.com/tag/"
For your URL
http://domain.com/tag/tagname/
The PHP function to get "tagname" is called basename():
echo basename('http://domain.com/tag/tagname/'); # tagname
combine some substring and some position finding after you take the last character off the string. use substr and pass in the index of the last '/' in your URL, assuming you remove the trailing '/' first.
As an alternative to the substring based answers, you could also use a regular expression, using preg_split to split the string:
<?php
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);
?>
(The reason for the -2 is because due to the ending /, the final element of the array will be a blank entry.)
And as an alternate to that, you could also use preg_match_all:
<?php
$ptn = "/[a-z]+/";
$str = "http://domain.com/tag/tagname/";
preg_match_all($ptn, $str, $matches);
$tagname = $matches[count($matches)-1];
echo($tagname);
?>
Many thanks to all, this code works for me:
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);

Categories