preg-replace

C# preg_replace?

Hi, What is the PHP preg_replace in C#? I have an array of string that I would like to replace by an other array of string. Here is an example in PHP. How can I do something like that in C# without using .Replace("old","new"). $patterns[0] = '/=C0/'; $patterns[1] = '/=E9/'; $patterns[2] = '/=C9/'; $replacements[0] = 'à'; $replacemen...

Replacing spaces using regex in php

I'm pretty new to regular expressions. I have a requirement to replace spaces in a piece of multi-line text. The replacement rules are these: Replace all spaces at start-of-line with a non-breaking space ( ) Replace any instance of repeated spaces (more than one space together) with the same number of non-breaking-spaces Single s...

Regex matching multiple occurrences per file and per line

Hi all, First post, so here goes. I'm writing a script that does intelligent search and replace on a file tree. Essentially, the script gets each file's contents into a buffer string and performs a match with a pre-defined pattern, in this case the pattern is /^[^\r\n]*(vendor)[^\r\n]*$/im. The pattern should find any case-insensitive f...

Very simple regular expression help

Hi Guys I'm very new to regex, can you help me with this. I have a string like this "<input attribute='value' >" where attribute='value' could be anything and I want to get do a preg_replace to get just <input /> How do I specify a wildcard to replace any number of any characters in a srting? like this? preg_replace("/<input.*>/",$r...

RegEx: matching and replacing ABC in "AB ABC D"

I need advice on this snippet $text = preg_replace('|(A.*)?A(.*)C|', '$1foo$2bar', $text); This will match ABC in "AB ABC D", and replace it with "AB fooBbar D"; as you can see this matches the "AB " part at the beginning as well, which I have to repeat in the replacement string with $1, in order not to lose it. Is this the best way ...

Regex to strip comments and multi-line comments and empty lines

Hello, I want to parse a file and I want to use php and regex to strip: blank or empty lines single line comments multi line comments basically I want to remove any line containing /* text */ or multi line comments /*** some text *****/ If possible, another regex to check if the line is empty (Remove blank lines) Is that pos...

preg_replace_callback() memory issue

i'm having a memory issue while testing a find/replace function. Say the search subject is: $subject = "I wrote an article in the A+ magazine. It'\s very long and full of words. I want to replace every A+ instance in this text by a link to a page dedicated to A+."; the string to be found : $find='A+'; $find = preg_quote($find...

Replacing Tags with Includes in PHP with RegExps

I need to read a string, detect a {VAR}, and then do a file_get_contents('VAR.php') in place of {VAR}. The "VAR" can be named anything, like TEST, or CONTACT-FORM, etc. I don't want to know what VAR is -- not to do a hard-coded condition, but to just see an uppercase alphanumeric tag surrounded by curly braces and just do a file_get_cont...

PHP: How can I get rid of commas inside double quotes in a multiple column row?

I need a PHP solution to get rid of commas inside double quotes. I can't seem to figure out a solution using preg_replace. I'm inserting data into a database from a text file that is coma delimited. Certain columns from the text file contain multiple words that are surrounded in double quotes. Theses double quotes have comas inside, so...

Multiple string replacements in the same string in php

I have the following string replacement problem and I am in quite a fix here PFB the sample string $string = 'The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.'; $patterns[0] = '/quick/'; $patterns[1] = '/quick/'; $patterns[2] = '/fox/'; $replacements[2] = 'bear'; $replacements[1] = 'black'; $replacement...

How can I replace each URL in a string with another unique URL?

I have the following: $reg[0] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si'; $reg[1] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si'; $replace[0] = '<a$1href="http://www.yahoo.com"$3&gt;'; $replace[1] = '<a$1href="http://www.live.com"$3&gt;'; $string = 'Test <a href="http://www.google.com"&gt;Google!!&lt;/a&gt;Test <a href="http://www.google.com"...

How to escape $ in php using preg_replace ?

Hi, I am using preg_replace to escape special characters... $tmpStr=preg_replace("/\?/", "\?", $tmpStr); $tmpStr=preg_replace("/\#/", "\#", $tmpStr); $tmpStr=preg_replace("/\^/", "\^", $tmpStr); $tmpStr=preg_replace("/\&/", "\&", $tmpStr); $tmpStr=preg_replace("/\*/", "\*", $tmpStr); ...

Replacing HTML attributes using a regex in PHP

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 s...

How to replace a variable within a string with PHP?

So I have some PHP code that looks like: $message = 'Here is the result: %s'; I just used %s as an example. It's basically a placeholder for whatever will go there. Then I pass the string to a function and I want that function to replace the %s with the value. What do I need to do to achieve this? Do I need to do some regex, and use ...

Regexp for cleaning the empty, unnecessary HTML tags

I'm using TinyMCE (WYSIWYG) as the default editor in one of my projects and sometimes it automatically adds <p>&nbsp;</p> , <p> </p> or divs. I have been searching but I couldn't really find a good way of cleaning any empty tags with regex. The code I've tried to used is, $pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/"; $str = preg_replace(...

MySQL string replace

Hey, what's the most effective way to remove beginning and ending slashes from all rows in a particular column using MySQL? Before: /hello/world/ foo/bar/ /something/else /one/more/* After: hello/world foo/bar something/else one/more/* ...or maybe this should be done in PHP instead? ...

Remove first forward slash in a link?

I need to remove the first forward slash inside link formatted like this: /directory/link.php I need to have: directory/link.php I'm not literate in regular expressions (preg_replace?) and those slashes are killing me.. I need your help stackoverflow! Thank you very much! ...

Regex Optional Groups?

I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated. My current regex is as follows: /<!--menu:start:\(([0-9])\,([0-9])\)-->(.*?)<!--menu:end-->/se I am looking to make the number input and colon e.g. :(1,4) optional, so it would match: <!--menu:start--><!--menu:end--> or ...

Why preg_replace throws me a "Unknown modifier" error ?

I keep getting this error: Warning: preg_match() [function.preg-match]: Unknown modifier 't' in D:\xampp\htdocs\administrator\components\com_smms\functions\plugin.php on line 235 on: $PageContent = preg_replace($result->module_pregmatch, '', $PageContent); I do a var_dump on the $result->module_pregmatch and I get the fo...

preg_replace Filter for Passwords

With PHP, I'd like to use a preg_replace() filter for passwords such that the only characters available for passwords are US ASCII typable, minus control codes and NULL. What's the RegEx to achieve that which I can plugin to preg_replace()? EDIT: I've been advised to edit this question since I "get it" now and won't be doing this terr...