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
Related
I want to remove the space between two words with a regex, however this does not seem to work.
$pattern = "#\<a href=\"(.+?) (.+?)\">#is";
$txt = preg_replace($pattern, "\<a href=\"\\1%20\\2\">", $txt);
I also need this to work for multiple words, but only withing the tags, as the rest of the text should have spaces. So a str_replace won't work (I think?)
Any tips?
The stable solution would be: Use DOM to retrieve the href value, use str_replace() to remove the spaces and then write back the value using DOM again.
Don't use regexes to handle html / xml.
Try this regex code to remove white spaces
\s+(?=[^()]*\
You can try the regex:
$txt = preg_replace('~(?:href="|(?<!^)\G)\K([^" ]*)\s+~g', "$1%20", $txt);
\G matches at the end of the previous match so that you can replace multiple spaces in a single attribute.
regex101 demo.
I've been looking all over the internet for some useful information and I think I found too much. I'm trying to understand regular expressions but don't get it.
Lets for instance say $data="A bunch of text [link=123] another bunch of text.", and it should get replaced with "< a href=\"123.html\">123< /a>".
I've been trying around a lot with code similar to this:
$find = "/[link=[([0-9])]/";
$replace = "< a href=\"$1\">$1< /a>";
echo preg_replace ($find, $replace, $data);
but the output is always the same as the original $data.
I think I have to see something relevent to my problem understand the basics.
Remove the extra [] around the (), and add + after the [0-9] to quantify it. Also, escape the [] that make up the tag itself.
$find = "/\[link=(\d+)\]/"; // "\d" is equivalent to "[0-9]"
$replace = "$1";
echo preg_replace($find,$replace,$data);
The regex would be \[link=([\d]+)\]
A good source for an quick overview of regular expression can you find here http://www.regular-expressions.info/
When you really interested in the power of regular expression, you should buy this book: Mastering Regular Expressions
A good Programm to test your RexEx on a Windows Client is: RegEx-Trainer
You are missing the + quantifier and as a result of this your pattern matches if there is a single digit following link=.
And there is an extra pair of [..] as a result of this the outer [...] will be treated as the character class.
You also forgot the escape the closing ].
Solution:
$find = "/[link=([0-9]+)\]/";
<?php
$data= "A bunch of text [link=123] another bunch of text.";
$find = '/\[link=([0-9]+?)\]/';
echo preg_replace($find, "$1", $data);
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) :-)
I currently have this regex:
$text = preg_replace("#<sup>(?:(?!</?sup).)*$key(?:(?!</?sup).)*<\/sup>#is", '<sup>'.$val.'</sup>', $text);
The objective of the regex is to take <sup>[stuff here]$key[stuff here]</sup> and remove the stuff within the [stuff here] locations.
What I actually would like to do, is not remove $key[stuff here]</sup>, but simply move the stuff to $key</sup>[stuff here]
I've tried using $1-$4 and \\1-\\4 and I can't seem to get the text to be added after </sup>
Try this;
$text = preg_replace(
'#<sup>((?:(?!</?sup).)*)'.$key.'((?:(?!</?sup).)*)</sup>#is',
'<sup>'.$val.'</sup>\1\2',
$text
);
The (?:...)* bit isn't actually a sub-pattern, and is therefor not available using backreferences. Also, if you use ' rather than " for string literals, you will only need to escape \ and '
// Cheers, Morten
You have to combine preg_match(); and preg_replace();
You match the desired stuff with preg_match() and store in to the variable.
You replace with the same regex to empty string.
Append the variable you store to at the end.
OK,I know that I should use a DOM parser, but this is to stub out some code that's a proof of concept for a later feature, so I want to quickly get some functionality on a limited set of test code.
I'm trying to strip the width and height attributes of chunks HTML, in other words, replace
width="number" height="number"
with a blank string.
The function I'm trying to write looks like this at the moment:
function remove_img_dimensions($string,$iphone) {
$pattern = "width=\"[0-9]*\"";
$string = preg_replace($pattern, "", $string);
$pattern = "height=\"[0-9]*\"";
$string = preg_replace($pattern, "", $string);
return $string;
}
But that doesn't work.
How do I make that work?
PHP is unique among the major languages in that, although regexes are specified in the form of string literals like in Python, Java and C#, you also have to use regex delimiters like in Perl, JavaScript and Ruby.
Be aware, too, that you can use single-quotes instead of double-quotes to reduce the need to escape characters like double-quotes and backslashes. It's a good habit to get into, because the escaping rules for double-quoted strings can be surprising.
Finally, you can combine your two replacements into one by means of a simple alternation:
$pattern = '/(width|height)="[0-9]*"/i';
Your pattern needs the start/end pattern character. Like this:
$pattern = "/height=\"[0-9]*\"/";
$string = preg_replace($pattern, "", $string);
"/" is the usual character, but most characters would work ("|pattern|","#pattern#",whatever).
I think you're missing the parentheses (which can be //, || or various other pairs of characters) that need to surround a regular expression in the string. Try changing your $pattern assignments to this form:
$pattern = "/width=\"[0-9]*\"/";
...if you want to be able to do a case-insensitive comparison, add an 'i' at the end of the string, thus:
$pattern = "/width=\"[0-9]*\"/i";
Hope this helps!
David