preg-replace

preg_replace: wildcards do not match umlaut-characters

i want to filter a String by using the \w wildcard, but unfortunately it does not cover umlauts. $i = "Die Höhe"; $x = preg_replace("/[^\w\s]/","",$i); echo $x; // "Die Hhe"; However, i can add all the characters to preg_replace, but this is not very elegant, since the list will become very long. ATM, i am preparing this only for ...

preg_replace - don't include string if $4 is blank

I have this expression: $regex_phone = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})' .'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})' .'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/'; if(!preg_match($regex_phone, $data['phone'])){ $error[] = "Please enter a valid ph...

How to replace <span style="font-weight: bold;">foo</span> by <strong>foo</strong> using PHP and regex?

I have a string like this: <span style="font-weight: bold;">Foo</span> I want to use PHP to make it <strong>Foo</strong> …without affecting other spans. How can I do this? ...

PHP - REGEX - use string for pattern but exclude it from being removed!

Hi All guys! i'm pretty new on regex, i have learned something by the way, but is still pour knowledge! so i want ask you for clarification on how it work! assuming i have the following strings, as you can see they can be formatted little different way one from another but they are very similar! DTSTART;TZID="America/Chicago":2003081...

PHP function similar to this JavaScript function

I need a similar function in PHP for this JavaScript function text = text.replace(/ffc/g, "Hello"); I think preg_replace will do, but i'm not sure how to write the expression.. I want the regular expression similar to "/ffc/g" which is above, What I need exactly is to match the full word and case when performing the replace... Thank...

Easy Regex question

Trying to replace the first 12 digits of credit card numbers with X's in a predictable blob of text that contains the string: Credit Card Number: 1234123412341234 Here's my PHP function: preg_replace('/Credit Card Number: ([0-9]{12})/','Credit Card Number: XXXXXXXXXXXX',$str); Help? ...

php - regex- preg_replace - space after line-break!

Hi all guys! still on regex! i want learn it but i'm still crashing the head into my keybord! ;-) ok very trivial for you, i'm sure! Assuming i have this sting, the \s is where the space actualy is... \n where linebreak is.. EDITED: OTHERFIELD: Other text here...`\n` DESCRIPTION: The quick brown fox jum`\s\n` `\s`ps over th...

preg_replace not working for charachter \

I'm trying to replace \' with ' but it won't work This is the text I want to replace from Using Twitter\'s @Anywhere Service in 6 Steps and this is the code $tutorial = "Using Twitter\'s @Anywhere Service in 6 Steps "; echo $tutorial."<br /><br />"; $tut_title = preg_replace("/\\'/", "'", $tutorial); echo $tut_title; ...

preg_replace, exact opposite of a preg_match

I need to do a preg_replace for the exact opposite of this preg_match regular expression: preg_match('#^(\w+/){0,2}\w+\.\w+$#', $string); So I need to replace all strings that are not valid with an empty string -> '' So it needs to remove the first / and last / if found, and all non-valid characters, that is the only valid characters...

Strip Javascript on(whatever) events from Code using PHP

Hi, I want to strip out all JavaScript from a small snippet (4-6 lines) of HTML, i've read on here before that its best not to use REGEX on HTML, so if anybody knows a better way, please advise. So for example i have the following code: <a href="go/to/my/link" onclick="fetchMeSomeData(this)">My Link</a> <p onfocus="doSomethingAmazing...

Regex to replace 'li' with 'option' without losing class and id attributes

I am looking for a solution using preg_replace or similar method to change: <li id="id1" class="authorlist" /> <li id="id2" class="authorlist" /> <li id="id3" class="authorlist" /> to <option id="id1" class="authorlist" /> <option id="id2" class="authorlist" /> <option id="id3" class="authorlist" /> I think I have the pattern corre...

PHP: BBCode with SQL selection?

I would like to code a bbcode with SQL selection. Basically I want the user to be able to input [user]Anotheruser[/user] in a text field, which then is converted in the front-end to an URL that looks like this: http://mydomain.com/user/[userid]/anotheruser. So in order to get the userid, I'd need to include an SQL selection, and addition...

Complex pattern replacement using PHP preg_replace function ignoring quoted strings

Consider the following string: this is a STRING WHERE some keywords ARE available. 'i need TO format the KEYWORDS from the STRING' In the above string keywords are STRING and WHERE Now i need to get an output as follows: this is a <b>STRING</b> <b>WHERE</b> some keywords ARE available. 'i need TO format the KEYWORDS from the ...

Replacing values using preg_replace

I have a Joomla plugin (not important in this context), which is designed to take an input with a load of numbers (within a paragraph of text) and replace them with a series of s. My problem is that I need to do a preg_replace on my $article->text, but I don't know how to then apply the changes to the matched terms. I've seen the preg_r...

php preg_replace, regexp

I'm trying to extract the postal codes from yell.com using php and preg_replace. I successfully extracted the postal code but only along with the address. Here is an example $URL = "http://www.yell.com/ucs/UcsSearchAction.do?scrambleSeed=17824062&keywords=shop&layout=&companyName=&location=London&searchType=advance&broaderLocation=&...

php preg_replace , I need to replace everything after a dot (e.g 340.38888 > need to get clean 340)

I need to replace everything after a dot . I know that it can be done with regex but I'm still novice and I don't understand the proper syntax so please help me with this . I tried the bellow code but doesn't work : $x = "340.888888"; $pattern = "/*./" $y = preg_replace($pattern, "", $x); print_r($x); thanks , Michael ...

preg_replace function to append a string to all the hyperlinks of a page

hi guys, i want to append my own value to all hyperlinks in a page... e.g if there are links: <a href="abc.htm?val=1">abc 1</a> <br/> <a href="abc.htm?val=2">abc 1</a> <br/> <a href="abc.htm?val=3">abc 1</a> <br/> <a href="abc.htm?val=4">abc 1</a> <br/> I want to add next var like "type=int" to all hyperlinks output should be: <a ...

Skip first regex match

Hi, Is there anyway to skip the first match when using regex and php. Or is there some way of achieveing this using str_replace. Thanks UPDATE I am trying to remove all the instances of a string from another string but I want to retain the first occurance e.g $toRemove = 'test'; $string = 'This is a test string to test to removing t...

Merge two files using cygwin tools

I have one tab separated file (file1.txt) with two columns and one file (file2.txt) with a long string of text. I want to replace specific characters in file2 with the column values in file1. An example: file1.txt text1 text11 text2 text22 text3 text33 file2.txt I want to insert text here:$1 and the other text here: $2 The desire...

Matching "Wibble A, B, C2, D" using PERL-regex

... Foo, Bar, Wibble A, B, C2, N, J, Baz, Qux, More, More, ... ... Bar, Qux, Wibble D, E, J, N6, O, Foo, Foo, More, More, ... and so on How do I match highlighted part of these strings using PERL-compatible regular expressions? It starts with a word "Wibble" and continues with one or two character components separated by a com...