I use preg_replace function. I want the function not to remove apostrophe (') character. So I want it to return the word as (o'clock) .
How can I do that?
$last_word = "o'clock.";
$new_word= preg_replace('/[^a-zA-Z0-9 ]/','',$last_word);
echo $new_word;
Try:
$last_word = "o'clock.";
$new_word= preg_replace('/[^a-zA-Z0-9\' ]/','',$last_word);
echo $new_word;
Demo here: http://ideone.com/JMH8F
That regex explicitly removes all characters except for letters and numbers. Note the leading "^". So it does what you ask it to.
So most likely you want to add the "'" (apostrophe) to the exclusion set inside the regex:
'/[^a-zA-Z0-9\' ]/'
Change your original '/[^a-zA-Z0-9 ]/' to "/[^a-zA-Z0-9 ']/". This simply includes the apostrophe in the negated character class.
See an online example.
Aside: my suggestion would be to use double-quotes for the string (as you have with "o'clock.") since mixing backslash escapes with PHP strings and regex patterns can get confusing quickly.
Try this. It may help..
$new_word= preg_replace('/\'/', '', $last_word);
Demo: http://so.viperpad.com/F82z9o
That regex you use does not remove the "'" (apostrophe). Instead it does not match the subject string at all because of the "." (dot). In that case preg_replace() returns NULL.
Related
$str="Your LaTeX document can \DIFaddbegin \DIFadd{test}\DIFaddend be easily
and the text can have multiple lines in it like this\DIFaddbegin \DIFadd{test2}
\DIFaddend"
I need to convert all \DIFaddbegin \DIFadd{test}\DIFaddend to \added{test}.
I tried
$o= preg_replace_callback('/\\DIFaddbegin\\s\DIFadd{(.*?)}\\DIFaddend/',
function($m) {return preg_replace('/$m[0]/','\added{$m[1]}',$m[0]);},$str);
But no luck. Which would be correct pattern for this? And also even if the string contains new line character the pattern should work.
You don't need a callback, using preg_replace() is fine for this task. To match a single backslash you need to double escape it meaning \\\\. To match possible whitespace between each substring, you can use \s* meaning whitespace "zero or more" times.
$str = preg_replace('~\\\\DIFaddbegin\s*\\\\DIFadd({[^}]*})\s*\\\\DIFaddend~', '\added$1', $str);
Try this:
$new_str = preg_replace("/\\\\DIFaddbegin \\\\DIFadd\{(.*)\}\\\\DIFaddend/s","\\added{\$1}",$str);
I am trying to remove parts of a string which has an ID and : before it. So for example:
2846:ZE1,2847:ZE2,2848:ZE3,713:DY10,412:CF10
But I want it to look like this:
ZE1,ZE2,ZE3,DY10,CF10
I have tried the following preg_replace:
$remove = preg_replace('/[0-9]\:+/', '', $postcodes_id);
But this only removes the last digit and not all of it:
284ZE1,284ZE2,284ZE3,71DY10,41CF10
any help would be great?
A non regex solution
parse_str(str_replace(array(':',','),array('=','&'),$str1),$str1);
Demo
Try this:
$remove = preg_replace('/[0-9]+\:/', '', $postcodes_id);
Adding the + means "one or more digit" instead of your code which is "just one".
I'm pretty sure you don't need the \ before the :...
You have the + in the wrong place, it should be:
preg_replace('/[0-9]+:/', '', $postcodes_id);
You also don't need to escape :, it has no special meaning in regular expressions.
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 would like to replace the following:
="Dark4Red"
with
=\"Dark4Red\"
The = sign and double quotations are fixed ="..."
I just need to add slashes before double quotations.
$content = preg_replace('/="[^a-zA-Z#0-9]"/', '=\"[^a-zA-Z#0-9]\"', $line);
The above solution didn't work. Any idea?
How about addslashes?
That aside, you appear to have not read any of the examples on preg_replace's manual page - if you had, you'd have known that you capture a subpattern (in this case the contents of the quotes) with parentheses and use $1 to put them in the replacement string.
You can use
$str = '"Text"';
str_replace('"','\"', $str);
$content = preg_replace('#="(.*?)"#', '=\"\1\"', $line);
Solved.
would like to replace all occurence of, double quote included
"http://somebunchofchar"
to
"link"
I came up with preg_replace("/\"http:\/\/.\"/i", "\"link\"", $string);
Just add an asterisk and question mark after dot
preg_replace("/\"http:\/\/.*?\"/i", "\"link\"", $string);
$string = preg_replace('#"http://.+"#', '"link"', $string);
You can use:
preg_replace('~"http://[^"]*"~i', '"link"', $string);
Just look here:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=1&categoryId=2&AspxAutoDetectCookieSupport=1
how to match an URL with the correct pattern; than use preg_replace with the particular regexp pattern ;-) (you can add those quotes at the start and end to the pattern yourself quite easily) :-)