I have a problem with preg_match in PHP.
I have URLs:
http://mysite.com/file/w867/2612512232
http://mysite.com/file/c3233/2123255464
etc.
I need URLs:
http://mysite.com/file/2612512232
http://mysite.com/file/2123255464
I must remove:
w867/
c3233/
etc.
You don't necessarily need to use preg_match. parse_url() can do the job.
http://us.php.net/manual/en/function.parse-url.php
Just make sure to concatenate it all against without the part you don't want.
you could try a pattern like this:
"/(.*)\/([^/])*\/(.*)/"
and then with str_replace you can: $string = str_replace('/'.$matches[2], '', $string);
preg_replace("|file/\w+/|", "file/", $url);
This will search for 1st pattern between "/" symbols just after "file/" part.
Related
Hi I have a Link like this:
mypage.php?product=3&page=1
I want to delete the &page=1, &page=2, &page=5 and so.
I have tried this, but I think it is not right.
str_replace('/(\\?|&)page=.*?(&|$)/', '', $link);
Thanks for your help.
str_replace() doesn't work with regular expressions, so you'd use preg_replace() instead:
$url = preg_replace('/[?&]page=[^&]+/', '', $url);
Two changes here: first, it's better to use a character class instead of alternation when you target individual symbol only (not having to escape ? within the brackets is a nice bonus), second, [^&]+ ('match any number of non-& characters') construct is more direct and readable than .+?(&|$) one.
I have the following string of img url's which I'm trying to sanitize
$img_string = http://image.s5a.com/is/image/saks/0401694719016_647x329.jpg," "="">/-/http://image.s5a.com/is/image/saks/0401694719016_A1_647x329.jpg," "="">
I'm exploding the string first like this
$img_array = explode('/-/', $img_string);
But I can't find a regex to remove everything after the last character in the image url.
e.g. regardless whether the img url ends in .png or .jpg or .jpeg, I need to just sanitize it.
My expected output is
http://image.s5a.com/is/image/saks/0401694719016_647x329.jpg
instead of
http://image.s5a.com/is/image/saks/0401694719016_647x329.jpg," "="">
So my question is, can someone help me with the required regex to achieve this?
Thanks
(?<=jpg|png|jpeg).*
Try this.Replace by empty string.See demo.
http://regex101.com/r/rQ6mK9/44
You can use preg_match using this regex:
[^,]+
RegEx Demo
Alternatively you can use this regex as well for preg_match:
^.+?\.(png|jpe?g)
Should work with this, deletes the comma from the URL:
if (preg_match_all("/(.*?),/is", $img_string, $matches)) {
$url = $matches[1][0];
echo $url;
}
Edit: tested here: http://ideone.com/pq1WzA
removing everything after the first comma like this :
$result = preg_replace('#[,].*$#ui', '', $img_string);
Hey I'm filtering a string and want it to go from:
512MBGDDR5videogeheugen
To:
512MB
So I tried php preg replace and did this:
$filterString = preg_replace("/[^0-9]+(KB|MB|GB)/", "", $string);
Does anyone know a way to solve this?
THANKS FOR THE RESPONSE!
Instead of replacing you can get your match like this.
preg_match("/^([0-9]+(KB|MB|GB))/", $string, $results);
$filterString = $results[0];
You can also use T-Regx library that has automatic delimiters:
pattern('^[0-9]+(KB|MB|GB)')->match($string)->all();
I'm actually not home so I tried a javascript regex, but I think it should work:
$filtered = preg_replace('^([0-9]+(KB|MB|GB))(.+)$','$1',$string)
there are some url, like 11162_sport.html, 11451_sport.html, 11245_sport.html or 231sport.html,
I want when the url like XXXXX_sport.html then replace them into 11162_football.html, 11451_football.html, 11245_football.html, and 231sport.html has no change.
how to replace them, $newurl = preg_replace("_sport.html","_football.html",$url)? Thanks.
Simply do $newurl = str_replace("_sport.html", "_football.html", $url);
This is faster than doing a preg_replace() and more accurant.
see the manual on str_replace.
you can use str_replace for such simple replacement.
If it must be regular expressions, do:
preg_replace('/_sport\.html$/', '_football.html', $url);
str_replace() would indeterminately replace all occurences of sport.html whereas a regular expression with an end-of-line marker ($) will only replace the pattern at the end of the URL.
The dot needs to be escaped because it would match any character (except new-lines).
I have a feeling that I might be missing something very basic. Anyways heres the scenario:
I'm using preg_replace to convert ===inputA===inputB=== to inputA
This is what I'm using
$new = preg_replace('/===(.*?)===(.*?)===/', '$1', $old);
Its working fine alright, but I also need to further restrict inputB so its like this
preg_replace('/[^\w]/', '', every Link or inputB);
So basically, in the first code, where you see $2 over there I need to perform operations on that $2 so that it only contains \w as you can see in the second code. So the final result should be like this:
Convert ===The link===link's page=== to The link
I have no idea how to do this, what should I do?
Although there already is an accepted answer: this is what the /e modifier or preg_replace_callback() are for:
echo preg_replace(
'/===(.*?)===(.*?)===/e',
'"$1"',
'===inputA===in^^putB===');
//Output: inputA
Or:
function _my_url_func($vars){
return ''.$vars[2].'';
}
echo preg_replace_callback(
'/===(.*?)===(.*?)===/',
'_my_url_func',
'===inputA===inputB===');
//Output: inputB
Try preg_match on the first one to get the 2 matches into variables, and then use preg_replace() on the one you want further checks on?
Why don't you do extract the matches from the first regex (preg_match) and treat thoses results and then put them back in a HTML form ?