preg-replace

preg_replace a specific pattern

I would like to use preg_replace to replace "* @license until */" with "testing". How can I do it? My text is the one below: /* * @copyright * @license * */ I hope everyone have understand my question correctly. ...

How do you replace repetitive text in PHP using preg_replace?

I often have comment forms and lots of users post things such as !!!!!!!!!!! and ??????????, and I want to use preg_replace to change them to !! and ??, with two maximum. Any idea how to do this? Thanks in advance. ...

Regex to remove div tags but not their content

Let's say this is my HTML: <ul> <li><div style="width: 10em;">Hello</div><div class="ble"></div></li> </ul> I want to get this: <ul> <li>Hello</li> </ul> As you can see, all div opening and closing tags were removed but not their content! This is what I have so far: $patterns = array(); $patterns[0] = '/<div.*>/'; $patter...

preg_replace - PHP

Afternoone guys and girls. having a bit of trouble with replacing things that are NOT 0-9A-Za-z[:space] because I cannot find a NOT metachar for preg_replace. Does anytone know if one exists and if not what is the best way to strip anything that is NOT alpha numeric or a space? Thanks in advance Alex ...

regexp find match

I'm working on a regexp to find and replace all matches that don't start with url(http relative paths are going to be converted to full absolut paths ie: url(foo/bar) > url('http://foo/bar') match: url(foo/bar) url('foo/bar') don't match: url(http://foo/bar) url('http://foo/bar') This is what I've come up with so far, but I'm n...

PHP, Preg_replace (regex), replacing just the part of pattern in ( )?

Sorry for this basic question, but I've been looking over all the info about preg_replace I can find and I still can't figure this out.. I have a large string, like this, for example: $string= '# tjs { fassdaf } #fsk { fssf} # fskff { casf }'; And when I do this, it replaces the entire pattern, not just the part in ( ) as I expect i...

Why is preg_replace giving this output?

I just can't figure this out, for some reason this: $string = "#mainparent { position: relative; top: 100px; left: 100px; width:4994px; }"; $elementwidth = "88"; $re1='(.*?)'; # Non-greedy match on filler $re2='(mainparent)'; # Word 1 $re3='(.*)'; # Non-greedy match on filler $re4='(width:)'; $re5='(.*)'; # Word 2 $re6=...

preg_replace not working for utf-8 Arabic text

I writing a php function to check existence of bad whole words (keep in mind whole word not sub-strings) and also highlight whole words in given string. function badwordChecherAndHighLighter($str,$replace){ // $replace=1 will Highlight // $replace=0 will Check the existence of any badwords $result = mysql_query("SELECT settings_ba...

preg_replace() with multiple lines

Dear all, I have trouble again with preg_replace again. I terrible need a book for preg_replace my intention is to replace <td> author </td> with <td> Level1 </td> Any ideas? the <td> author </td> is wrapped with other HTML element <td align="center"> <a onclick="return listItemTask('cb1','block')" href="javascript:void(0);"> ...

Multi-line Links with preg_replace | PHP

If someone posts a multi-line post that contained text and links, I want to be able to find and wrap the links with <p> tags, but I can only do it with one link at a time (source code comes from phpBB2 - clickable links function), which causes every link to be like this: <p>http://www.bbc.co.uk/&lt;/p&gt; <p>http://www.bbc.co.uk/&lt;/p&...

Codeigniter and preg_replace

I use Codeigniter to create a multilingual website and everything works fine, but when I try to use the "alternative languages helper" by Luis I've got a problem. This helper uses a regular expression to replace the current language with the new one: $new_uri = preg_replace('/^'.$actual_lang.'/', $lang, $uri); The problem is that I ha...

How do you write a Wordpress function to put a Span around the first word of the Title?

I want to replace first word in title to have <span></span> inside. Example for Wordpress title <h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2> I want to be like this <h2 class="entry-title"><a href="#"><span>Welcome</span> to Wordpress</a></h2> the function function span_on_title($span) { return preg_replace('...

Efficiency of Preg_replace

Executive Summary: preg_replace() ran faster than string comparisons. Why? Shouldn't regular expressions be slower? In a recent question about detecting any of an array of disallowed substrings within a given input, I suggested comparing the result of a preg_replace() call to the original input, since preg_replace() can take an array...

Remove trailing newline

I've a MySQL database from which I extract a string which is a list of words separated by newline. Now I want to remove only the trailing newline. I tried using preg_replace as $string = preg_replace('/\n/','',$string); It works but all the newlines in the strings are removed :( Please help Thanks everyone. ...

Regular expressions and php

Say I have a string like this: $string = 'The /*quick*/ brown /*fox*/ jumped over the lazy /*dog*/.'; How can I use a regular expression to find the occurrences of /* */ and replace each value like so: /*quick*/ with the value of $_POST['quick'] /*fox*/ with the value of $_POST['fox'] /*dog*/ with the value of $_POST['dog'] I have ...

preg_replace apply string function (like urlencode) in replacement

hi! i want to parse all links in html document string in php in such way: replace href='LINK' to href='MY_DOMAIN?URL=LINK', so because LINK will be url parameter it must be urlencoded. i'm trying to do so: preg_replace('/href="(.+)"/', 'href="http://'.$host.'/?url='.urlencode('${1}').'"', $html); but '${1}' is just string literal, no...

Preg_Replace and Capture Value? PHP

I'm building a filter plugin in Wordpress and I'm replacing some plugin specific tags with bits of html. Example: [VIDEO ID=12] will be replaced via preg_replaced in this function function display_video($text){ $pattern = '/\[VIDEO ID\=\d+\]/'; $text=preg_replace($pattern,get_video_block($id),$text); return $text; } I'm n...

PHP: Add Unique ID + Order ID for each word what starts with @

I need to add an Order ID + Unique ID for each word what starts with @. For example I have a string like this: Just @do @it and @do @it. I want to preg_replace #(\@)+([^\s]+)#i to this: Just <div id="1+Unique ID">@do</div> <div id="2+Unique ID">@it</div> and <div id="3+Unique ID">@do</div> <div id="4+Unique ID">@it</div>. ...

preg_replace ".php"?

Hi. How do I preg_replace .php from end of string? for ($x = 0; $x < count($sidebar_links); $x++) { $sidebar[$x] = $x; } $sidebar_links: array(3) { [0]=> string(9) "index.php" [1]=> string(9) "site2.php" [2]=> string(7) "site3.php" } ...

Replacing file content in PHP

I need a function just like preg_replace but instead of strings I need it to work with files / file content. ...