php regex to replace last dot - preg-replace

http://i.imgur.com/KKtAU8X.jpg
preg_replace to change be like
http://i.imgur.com/KKtAU8Xt.jpg
replace last dot to t.
try code
/\.([^.]*)$/ or /(.*)\.([^.]*)$/
add it remove .jpg too!
I need to keep .jpg

$ cat test.php
<?
$url = 'http://i.imgur.com/KKtAU8X.jpg';
$newUrl = preg_replace('#^(.+)\.([\w]+)$#i', '$1t.$2', $url);
var_dump($url, $newUrl);
$ php test.php
string(30) "http://i.imgur.com/KKtAU8X.jpg"
string(31) "http://i.imgur.com/KKtAU8Xt.jpg"

Related

Why preg_replace() function isn't working properly?

My PHP Script is:
<?php
$string = '{controller}/{action}';
$pattern = '/\{([a-z]+)\}/i';
$replacement = '(?P<$1>[a-z-]+)';
echo preg_replace($pattern, $replacement, $string);
?>
it is showing this result:
(?P[a-z-]+)\/(?P[a-z-]+)
I am expecting this:
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
How I can able to do this??
Your code produces the correct result, that is,
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
The problem is: when you echo that out and display it in a browser, the browser interprets <controller> and <action> as HTML tags, like <p> or <strong>. So, it doesn't display them; it only displays what is left:
(?P[a-z-]+)\/(?P[a-z-]+)
You would see the correct result if you ran this script from the command line. To make it work in the browser, you need to replace the last line with
echo htmlentities(preg_replace($pattern, $replacement, $string));

PHP Remove specific line return

After i've done a preg_replace to remove the TAG 'TagToremove', i still have a line return, would you know if either i could remove it during the preg_replace
Or After and how ?
Thanks
<?php
$strip_list = array('TagToremove');
foreach ($strip_list as $tag)
{
$temp = preg_replace('/<\/?' . $tag . '(.|\s)*?>/', '', $temp);
}
?>
String before Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<TagToremove>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
</TagToremove>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
String after Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
Don't parse HTML with regex, use a DOM parser instead.
Nevertheless, for your specific problem, you have to add an optional linebreak in your regex like that:
$temp = preg_replace('~</?' . $tag . '[^>]*>\R?~i', '', $temp);
\R stands for any kind of linebreak (ie. \r or \n or \r\n)

PHP DOMDocument - match and remove URLs

I'm trying to extract links from html page using DOM:
$html = file_get_contents('links.html');
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$a = $DOM->getElementsByTagName('a');
foreach($a as $link){
//echo out the href attribute of the <A> tag.
echo $link->getAttribute('href').'<br/>';
}
Output:
http://dontwantthisdomain.com/dont-want-this-domain-name/
http://dontwantthisdomain2.com/also-dont-want-any-pages-from-this-domain/
http://dontwantthisdomain3.com/dont-want-any-pages-from-this-domain/
http://domain1.com/page-X-on-domain-com.html
http://dontwantthisdomain.com/dont-want-link-from-this-domain-name.html
http://dontwantthisdomain2.com/dont-want-any-pages-from-this-domain/
http://domain.com/page-XZ-on-domain-com.html
http://dontwantthisdomain.com/another-page-from-same-domain-that-i-dont-want-to-be-included/
http://dontwantthisdomain2.com/same-as-above/
http://domain3.com/page-XYZ-on-domain3-com.html
I would like to remove all results matching dontwantthisdomain.com, dontwantthisdomain2.com and dontwantthisdomain3.com so the output will looks like that:
http://domain1.com/page-X-on-domain-com.html
http://domain.com/page-XZ-on-domain-com.html
http://domain3.com/page-XYZ-on-domain3-com.html
Any ideas? :)
I think you should use regular expression.Google it and have fun

How to use preg_replace instead of eregi php function

When I moved to a new server, eregi function generates too many errors. I like to use preg_replace instead of eregi function.
Here is my block of code:
if($rstate && !$rcity && !eregi("outside",$rstate)){
$h1title=str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
echo str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
I changed it to:
if($rstate && !$rcity && !preg_replace("outside",$rstate)){
$h1title=str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
echo str_replace('DDD',$tsstate,str_replace('XXX',$tsstate,$pagetitlet));
But it is looking for another value. How do I add the third parameter for preg_replace to get it worked?
Thanks.
You don't need to use a regex for this test, use strpos instead:
if ($rstate && !$rcity && strpos($rstate, "outside")===false) {
$h1title = str_replace('DDD', $tsstate, str_replace('XXX',$tsstate,$pagetitlet));
echo str_replace('DDD', $tsstate, str_replace('XXX',$tsstate,$pagetitlet));
...
}
Note that the regex function you are looking for is preg_match

PHP preg_replace with any variation of upper/lowercase?

I needed to write a custom module in drupal to help out with my location search. Initially I simply needed to remove a comma from queries, and then I realized that I would need to replace all instances of states with their abbreviation (California -> CA) because of how information is stored in my database. However, upon doing this I found out that my method of using preg_replace seems to be dependent on upper/lowercase. So in this line:
$form_state['values'] = preg_replace("/alabama/", 'al', $form_state['values']);
"alabama" will be replaced with "al", but "Alabama" or "ALABAMA" will not. Is there a way to replace any instance of Alabama with its abbreviation without accounting for every possible variation in casings?
you can try also str_ireplace() it's Case-insensitive
<?php
$str = 'alabama ,Alabama,ALABAMA';
$replace = str_ireplace('alabama','al',$str);
echo $str;
echo "<br/>";
echo $test;
?>
$form_state['values'] = preg_replace("/alabama/i", 'al', $form_state['values']);
The 'i' modifier will make the pattern case-insensitive.