I want to replace a string by different patterns depending on it format. For example:
The pattern Y-m-d should be replaced by \d{4}-\d{2}-\d{2},
m-d-Y should be \d{2}-\d{2}-\d{4} and so on.
I used preg_replace() two times for doing it. One for replacing \m|d\ and another for \Y\.
$pattern = preg_replace("/m|d/", "\d{2}", $format);
$pattern = preg_replace("/Y/", "\d{4}", $pattern);
I want to know how can I do it by singe regex. Your help will be appreciated.
Thanks
preg_replace can act on array:
$pat = array('/m/', '/d/', '/Y/');
$repl = array('\\d{2}', '\\d{2}', '\\d{4}');
$pattern = preg_replace($pat, $repl, $pattern);
Related
I'm trying to use preg_match just for string inside two certain symbols for example
My Name is %^i%Ibrahem ^i.
I want to use preg_match just for ^i which is inside this two symbols %% to be font-style:italic; I've tried:
$find=array('`\^i`si');
$replace=array('font-style:italic;');
$replaced = preg_replace($find,$replace,$string);
but it replaces the last ^i too also keep in mind that the string between %% can be also %^b ^i% so I can't condition that the string have to be %^i% Help!
You could use this pattern instead:
$find = array('/%(.*?)(\^i)(.*?)%/', '/%(.*?)(\^b)(.*?)%/');
$replace = array('%$1font-style:italic;$3%', '%$1font-weight:bold;$3%');
$replaced = preg_replace('/%(.*)%/', '<span style="$1">', preg_replace($find, $replace, $string));
i've scraped a html string from a website. In this string it contains multiple strings like color:#0269D2. How can i make str_replace code which replace this string with another color ?
For instance something like this just looping through all color:#0269D in the fulltext string variable?
str_replace("color:#0269D","color:#000000",$fulltext);
you pass array to str_replace function , no need to use loop
$a= array("color:#0269D","color:#000000");
$str= str_replace($a,"", $string);
You have the right syntax. I would add a check:
$newText = str_replace("color:#0269D", "color:#000000", $fulltext, $count);
if($count){
echo "Replaced $count occurrences of 'color'.";
}
This code might be too greedy for what you're looking to do. Careful. Also if the string differs at all, for example color: #0269D, this replacement will not happen.
’str_replace’ already replaces all occurrences of the search string with the replacement string.
If you want to replace all colors but aren't sure which hexcodes you'll find you could use preg_replace to match multiple occurrences of a pattern with a regular expression and replace it.
In your case:
$str = "String with loads of color:#000000";
$pattern = '/color ?: ?#[0-9a-f]{3,6}/i';
$replacement = "color:#FFFFFF";
$result = preg_replace($pattern, $replacement, $str);
I've searched for an example of this, but can't seem to find it.
I'm looking to replace everything for a string but the #texthere
$Input = this is #cool isn't it?
$Output = #cool
I can remove the #cool using preg_replace("/#(\w+)/", "", $Input); but can't figure out how to do the opposite
You could match #\w+ and then replace the original string. Or, if you need to use preg_replace, you should be able to replace everything with the first capture group:
$output = preg_replace('/.*(#\w+).*/', '\1', $input);
Solution using preg_match (I assume this will perform better):
$matches = array();
preg_match('/#\w+/', $input, $matches);
$output = $matches[0];
Both patterns above do not address the issue how to handle inputs which match multiple times, such as this is #cool and #awesome, right?
I have this code:
<object height="510" width="650">height="510"width="650">
or this code:
<objectheight="345"width="123'> height='456'width=789>
The quotes around the values could be double, single or none. And the words could be put together or there could be one or more whitespaces. And what is important the digits as a value could be anything
So, I need to replace the integervalue in height="510" (or height='123' or height=456) with my own variable $height_val.
My code so far:
$height_val = "640";
$pattern = ??? // this regex I need to find out
$replacement = $height_val;
$string = '<objectheight="345"width="123\'> height=\'456\'width=789>'
$result = preg_replace($pattern, $replacement, $string);
And the final result should be <objectheight="640"width="123\'> height=\'640\'width=789>
The reg-ex I'd use is: height=(["']*)[0-9]*(["']*). This ensures you only get the height value with any non-alpha digit after the equals followed by an any length number.
$height_val = "640";
//$pattern = '/height=\D[0-9]*\D/' // pre comments regex
$pattern = height='/(["']*)[0-9]*(["']*)/'; //new regex
$replacement = $height_val;
$string = '<objectheight="345"width="123\'> height=\'456\'width=789>'
$result = preg_replace($pattern, $replacement, $string);
I've tested it on the following variables:
<object height="510" width="650">height="510"width="650">
<objectheight="510" width="650">height="510"width="650">
<object height='510' width="650">height="510"width="650">
<objectheight='510'width="650">height="510"width="650">
value="width=650&height=515&plugins=http://
Going forward I would recommend you try using a RegEx tester to try your own combinations. You can also use this regex reference to give you help with character classes.
Updated:
If you wish to allow for no quotation marks too use the following:
I have a URL like this:
http://Example.com/mobile-ds-cams/mobile-gg-cams/ddd-webcams
Example:
$pattern = '/http://Example.com/(\w+)/(\w+)/(\w+)/i';
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);
What I want to achieve is this
http://Example.com/mobile-gg-cams/ddd-webcams
I am trying to keep 2 "sub-URLs" instead of 3. but it doesn't work..why?
You need to escape your forward-slashes within the pattern, or use different pattern delimiters.
$pattern = '/http:\/\/Example\.com\/(\w+)\/(\w+)\/(\w+)/i';
$pattern = '#http://Example\.com/(\w+)/(\w+)/(\w+)#i';
It doesn't work correctly because your expression contains characters with special meaning in a regex that have not been properly quoted.
To be 100% certain, use preg_quote like this:
$url = 'http://Example.com/'
$pattern = preg_quote($url.'{word}/{word}/{word}', '/');
$pattern = str_replace($pattern, '{word}', '(\w+)');
$pattern = "/$pattern/i";
$replacement = $url.'$2/$3';
$appUrl= preg_replace($pattern, $replacement, $appUrl);
Otherwise, it's simply too easy to get things wrong. For example, all of the other answers currently posted here get it wrong because they do not properly escape the . in Example.com. You can test this yourself if you feed them a string like e.g. http://Example!com, where ! can be any character you like.
Additionally, you are using strings such as $2 inside a double-quoted string literal, which is not a good idea in PHP because IMHO it's easy to get carried away. Better make that singly quoted and be safe.
Escape the slashes like this:
$pattern = '/http:\/\/Example.com\/(\w+)\/(\w+)\/(\w+)/i';
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);