Instead of wondering what regex I need for each time I need a regex, I like to learn how to do basic string replacement with regexes.
How do you take one string and replace it with another with regexes in PHP ?
For example how do you take all '/' and replace them with '%' ?
If you just want to do basic string replacement (i.e. replace all 'abc' with '123') then you can use str_replace, which doesn't require using regex. For basic replacements, this will be easier to set up and should run faster.
If you want to use this as a tool to learn regex though (or need more complicated replacements) then preg_replace is the function you need.
You should look into preg_replace. For your question
$string = "some/with/lots/of/slashes";
$new_string = preg_replace("/\//","%",$string);
echo $new_string;
I like to learn how to do basic string replacement with regexes.
That's a little broad for this forum. However, to answer a more specific question like:
For example how do you take all '/' and replace them with '%' ?
You could do that like this:
$result = preg_replace('#\/#', '%', 'Here/is/a/test/string.');
Here is a Rubular that proves the regex.
Note, you are not limited to using the common / delimiter, which means when working with forward slashes it is often easier to change to a different delimiter EG.
$mystring = 'this/is/random';
$result = preg_replace('#/#', '%', $mystring);
This will make '#' the delimiter, rather than the standard '/', so it means you do not need to escape the slash.
I would do this simply with strtr which is very suitable for character mapping, e.g.
strtr('My string has / slashes /', '/', '%');
If you want to also replace the letter a with a dash:
strtr('My string has / slashes /', '/a', '%-');
As you can see, the second and third argument define the transformation map.
Related
I use the following regexp in a php function to replace URLs with proper HTML links:
return preg_replace('#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#', '$1', $s);
But when $s has for value a string like
<li>http://www.link.com/something.pdf</li>
the function returns
<li>http://www.link.com/something.pdf</li></li>
Does anyone know how to modify the regexp to get the intended string, i.e.
<li>http://www.link.com/something.pdf</li> ?
without excluding from the replacement substrings of the URL introduced by '%', '?' or '&' ?
Really easy solution:
return '<li>'.preg_replace('#(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\#!$&\'()*+,;=%]*)?)?)#', '$1', $s).'</li>';
If you really want a regex:
return preg_replace('#(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\#!$&\'()*+,;=%]*)?)?)#', '$1', $s);
You rpattern is not sufficient (to catch all the links), but anyway, instead of \S+ you might want to have [^\s<>]+ because the former catches everything non-space.
Same applies to [^\.\s]. Make this [^\s<>.]. You don't need to escape the dot when used in a character class, so my addition to this group was basically the greater than and less than signs.
I want to be able to search for a certain word in a string, and append and prepend characters to every instance of that word.
example:
I like cats, cats are awesome! I wish I had cats!
becomes:
I like (cats), (cats) are awesome! I wish I had (cats)!
I know I could use
str_replace( 'cats', '(cats)', $string );
but I would have to write "cats" twice.
I want a method that only requires me to write it once.
$search = 'cats';
preg_replace('/' . preg_quote($search, '/') . '/', '($0)', $string);
Paraphrased from the preg_replace documentation:
The replacement string may contain references of the form $n. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. $0 refers to the text matched by the whole pattern.
use a preg_replace() with back references: http://php.net/manual/en/function.preg-replace.php
preg_replace("/cats/smi","($0)",$string);
You commented:
I have a huge plain text list of values I need to parenthesize, and I
wanted to make it easier to write the code.
Since you're going to be doing many replacements (expensive cpu-wise), and you're wanting to simply shorten your coding time, why not also save CPU by wrapping str_replace()?
function str_wrap($needle,$prefix,$suffix,$haystack) {
return str_replace($needle, $prefix . $needle . $suffix, $haystack);
}
Per the PHP manual:
If you don't need fancy replacing rules (like regular expressions),
you should always use [str_replace()] instead of preg_replace().
i'v got such string <>1 <>2 <>3
i want remove all '<>' and symbols after '<>' i want replace with such expression like www.test.com/1.jpg, www.test.com/2.jpg, www.test.com/3.jpg
is it possible to do with regex? i only know to find '/<>.?/'
preg_replace('/<>(\d+)/g', 'www.test.com/bla/$1.jpg', $input);
(assuming your replaced elements are just numbers. If they are more general, you'll need to replace '\d+' by something else).
str_replace('<>', 'www.test.com/', $input);
// pseudo code
pre_replace_all('~<>([0-9]+)~', 'www.test.com/$1.jpg', $input);
$string = '<>1 <>2 <>3';
$temp = explode(' ',preg_replace('/<>(\d)/','www.test.com/\1.jpg',$string));
$newString = implode(', ',$temp);
echo $newString;
Based on your example, I don’t think you need regex at all.
$str = '<>1 <>2 <>3';
print_r(str_replace('<>', 'www.test.com/', $str));
Regex's allow you to manipulate a string in any fashion you desire, to modify the string in the fashion you desire you would use the following regex:
<>(\d)
and you would use regex back referencing to keep the values you have captured in your grouping brackets, in this case a single digit. The back reference is typically signified by the $ symbol and then the number of the group you are referencing. As follows:
www.test.com/$1
this would be used in a regex replace scenario which would be implemented in different ways depending on the language you are implementing your regex replace method in.
Is there a canonical function/method for escaping a string to be used in a preg_, such that any special PCRE characters will be interpreted as literal. Basically, a know way to ensure that something like
I am a fancy string (well, that guy ... said I was fancy)
is transformed into
I am a fancy string \(well, that guy \.\.\. said I was fancy\)
The use case is something like
$re = get_string_from_somewhere();
$re = our_magic_function($re);
preg_match_all('%'.$re.'%',$string, $matches);
I believe that preg_quote() is the answer you're looking for...
If you're using a custom delimiter (as you do in your example), be sure to set the second parameter ($delimiter) to the one used in the regex... So your call would be preg_quote($string, '%');
Since I am completely useless at regex and this has been bugging me for the past half an hour, I think I'll post this up here as it's probably quite simple.
hey.exe
hey2.dll
pomp.jpg
In PHP I need to extract what's between the <a> tags example:
hey.exe
hey2.dll
pomp.jpg
Avoid using '.*' even if you make it ungreedy, until you have some more practice with RegEx. I think a good solution for you would be:
'/<a[^>]+>([^<]+)<\/a>/i'
Note the '/' delimiters - you must use the preg suite of regex functions in PHP. It would look like this:
preg_match_all($pattern, $string, $matches);
// matches get stored in '$matches' variable as an array
// matches in between the <a></a> tags will be in $matches[1]
print_r($matches);
This appears to work:
$pattern = '/<a.*?>(.*?)<\/a>/';
([^<]*)
I found this regular expression tester to be helpful.
Here is a very simple one:
<a.*>(.*)</a>
However, you should be careful if you have several matches in the same line, e.g.
hey.exehey2.dll
In this case, the correct regex would be:
<a.*?>(.*?)</a>
Note the '?' after the '*' quantifier. By default, quantifiers are greedy, which means they eat as much characters as they can (meaning they would return only "hey2.dll" in this example). By appending a quotation mark, you make them ungreedy, which should better fit your needs.