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.
Related
I have an CSV File with the following content:
"Title","Firstname","Lastname","Description"
"Mr","Peter","Tester",,
"Mrs",,"Master","Chief, Supporter"
"Mr","Seg, Jr.","Tuti","Head, Developer"
Now I want to remove the quote sourrounded comma by preg_replace ("Chief, Supporter"; "Seg, Jr."; "Head, Developer").
But I am not able to build a suitable Regex.
My last result looks like: /\"(.[^\",]*),(.[^\"]*)\"/i
Your requirements are a little unclear, but if I understand correctly, you want to remove the comma if one exists within a double-quoted string so that
e.g. "Head, Developer" becomes "Head Developer", etc
Based on that assumption then
/\"([^\"]+?),+ *(\w[^\"]+?)\"/gmi
will find those commas, and
"$1 $2"
will replace it with a space.
see demo here
PHP example (I'm not very conversant with php so the character escaping, etc might need tweaking)
<?php
$string = '"Mr","Seg Jr.","Tuti","Head Developer"';
$pattern = '/\"([^\"]+?),+ *(\w[^\"]+?)\"/gmi';
$replacement = '"$1 $2"';
echo preg_replace($pattern, $replacement, $string);
?>
Use this regex: ((?>\*\*)),((?>\*\*))
Capture the ** in parentheses and replace with ,, see DEMO
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.
I have a PHP page which gets text from an outside source wrapped in quotation marks. How do I strip them off?
For example:
input: "This is a text"
output: This is a text
Please answer with full PHP coding rather than just the regex...
This will work quite nicely unless you have strings with multiple quotes like """hello""" as input and you want to preserve all but the outermost "'s:
$output = trim($input, '"');
trim strips all of certain characters from the beginning and end of a string in the charlist that is passed in as a second argument (in this case just "). If you don't pass in a second argument it trims whitespace.
If the situation of multiple leading and ending quotes is an issue you can use:
$output = preg_replace('/^"|"$/', '', $input);
Which replaces only one leading or trailing quote with the empty string, such that:
""This is a text"" becomes "This is a text"
$output = str_replace('"', '', $input);
Of course, this will remove all quotation marks, even from inside the strings. Is this what you want? How many strings like this are there?
The question was on how to do it with a regex (maybe for curiosity/learning purposes).
This is how you would do that in php:
$result = preg_replace('/(")(.*?)(")/i', '$2', $subject);
Hope this helps,
Buckley
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) :-)
There has always been a confusion with preg_match in php.
I have a string like this:
apsd_01_03s_somedescription
apsd_02_04_somedescription
Can I use preg_match to strip off anything from 3rd underscore including the 3rd underscore.
thanks.
Try this:
preg_replace('/^([^_]*_[^_]*_[^_]*).*/', '$1', $str)
This will take only the first three sequences that are separated by _. So everything from the third _ on will be removed.
if you want to strip the "_somedescription" part: preg_replace('/([^]*)([^]*)([^]*)(.*)/', '$1_$2_$3', $str);
I agree with Gumbo's answer, however, instead of using regular expressions, you can use PHP's array functions:
$s = "apsd_01_03s_somedescription";
$parts = explode("_", $s);
echo implode("_", array_slice($parts, 0, 3));
// apsd_01_03s
This method appears to execute similarly in speed, compared to a regular expression solution.
If the third underscore is the last one, you can do this:
preg_replace('/^(.+)_.+?)$/', $1, $str);