preg-replace

preg_match to match an optional string, but not match all of the string

Take for example the following regex match. preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); p...

Regexp: Replace only in specific context

In a text, I would like to replace all occurrences of $word by [$word]($word) (to create a link in Markdown), but only if it is not already in a link. Example: [$word homepage](http://w00tw00t.org) should not become [[$word]($word) homepage](http://w00tw00t.org). Thus, I need to check whether $word is somewhere between [ and ] and on...

preg_replace: unknown modifier

Let assume that $body is equal to something that does not interest me <!-- start --> some html code <!-- end --> something that does not interest me If I use $body=preg_replace("(.*)<!-- start -->(.*)<!-- end -->(.*)","$2",$body); I obtain: Warning: preg_replace() [function.preg-replace]: Unknown modifier '<' How have I t...

Multiple calls to preg_replace alters result

I have a bunch of files that were named in a somewhat standard format. The standard form is basically this: [integer]_word1_word2_word3_ ... _wordn where a word could really be anything, but all words are separated by an underscore. There is really only 3 things I want to do to the text: 1.) I want to modify the integer, which is al...

Extracting Text from between parentheses in PHP using preg_replace

I am trying to take a string of text like so: $string = "This (1) is (2) my (3) example (4) text"; In every instance where there is a positive integer inside of parentheses, I'd like to replace that with simply the integer itself. The code I'm using now is: $result = preg_replace("\((\d+)\)", "$0", $string); But I keep getting a ...

Assistance with PHP Regex

Hi All I don't usually use regular expressions, hence my question. I need a regex to match the following: '{any-string}'. Any assistance appreciated. ...

PHP preg_replace - Don't match within h1 tags

Hi there. I am using preg_replace to add a link to keywords if they are found within a long HTML string. I don't want to add a link if the keyword is found within h1 tags or strong tags. The below regex nearly works and basically says (I think): If the keyword is not immediately wrapped by either a h1 tag or a strong tag then replace wi...

Preg_match if a string beginns with "00"{number} or "+"{number}

Hy i have to test if a string begins with 00 or with + Say i have the string 0090 or +41 if the string begins with 0090 return true, elseif string begins with +90 replace the + with 00 else return false The last two digits can be from 0-9 How do i do that in php? I hope i could explain my question clear? ...

preg_replace pass match through function before replacing

This is what i want to do: $line = 'blabla translate("test") blabla'; $line = preg_replace("/(.*?)translate\((.*?)\)(.*?)/","$1".translate("$2")."$3",$line); So the result should be that translate("test") is replaced with the translation of "test". The problem is that translate("$2") passes the string "$2" to the translate function. ...

Selectively replacing words outside of tags using regular expressions in PHP?

I have a paragraph of text and i want to replace some words using PHP (preg_replace). Here's a sample piece of text: This lesson addresses rhyming [one]words and ways[/one] that students may learn to identify these words. Topics include learning that rhyming words sound alike and these sounds all come from the [two]ending of the words[/...

PHP preg_replace - Very quick question

Masters of regular expressions, please help! See this string: $string = "http://www.url.com/?fieldA=123&amp;fieldB=456&amp;fieldC=789"; Assuming "fieldB" always has a positive non-decimal numerical value (but not necessarily three digits long), what preg_replace command do I need to remove it completely, such that the string will the...

PHP str_replace and preg_replace work on one server but not another

UPDATE: As it turns out, the below is caused by a caching issue on my production server. Thanks to everybody who contributed thoughtful answers. I have a simple function on a php page that takes a url such as: http://myurl.com/mypage.html?param1=value1 and converts it to: http://myurl.com/searchpage.html?param1=value1 All it does it...

PHP: Rawurlencode inside Preg-replace

Hello, I have this snippet while($row = mysql_fetch_array($result)){ $search = '' . $row['searchquery']; echo '<a href="http://www.example.com/' . preg_replace( array('/[^\s\w]/','/\s/'),array('','+'),$search) . '+/">' ... but if someone types in äöü, it doesnt show the letters - with rawurlencode thats possible, but I want to remov...

Highlight text, except html tags

Hey, I'm using the code below to highlight some keywords in a text: $message = str_ireplace($words,'<span class="hightlighted_text">'.$words.'</span>',$message); The text may contain some html tags, for example , etc.. How can I highlight "normal" text, except the text between the html tags? Because when users search for "img" the ...

Replacing keywords in text with php & mysql

Hello, I have a news site containing an archive with more than 1 million news. I created a word definitions database with about 3000 entries, consisting of word-definition pairs. What I want to do is adding a definition next to every occurence of these words in the news. I cant make a static change as I can add a new keyword everyday, ...

How do I get preg replace to work when the url contains a hashtag

I found a function online for turning a url within a string into a clickable link. However, when the url contains a hashtag it doesn't work. eg. http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759 Here's the part of the function concerned: $ret = preg_replace( "#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a ...

Removing most inline styles and properties with PHP

This question is related to a similar case, namely http://stackoverflow.com/questions/2488950/removing-inline-styles-using-php The solution there does not remove i.e: <font face="Tahoma" size="4"> But let's say I have a mixed bag of inline styles and properties, like this: <ul style="padding: 5px; margin: 5px;"> <li style="padding...

Finding and replacing content between brackets with jQuery

I have a requirement to place text between brackets using jQuery; the content will be updated once an action occurs e.g. search. The structure is: - <h2>Placeholder Text Placeholder Text Placeholder Text ()</h2> ...

Is there a way to pass another parameter in the preg_replace_callback callback function?

mmmh guys, i really hope my english is good enaught to explain what i need. Lets take this example (that is just an example!) of code: class Something(){ public function Lower($string){ return strtolower($string); } } class Foo{ public $something; public $reg; public $string; public function __construct(...

php regex expression to get title

I'm trying to strip content titles out of the middle of text strings. Could I use regex to strip everything out of this string except for the title (in italics) in these strings? Or is there a better way? Joe User wrote a blog post called The 10 Best Regex Expressions in the category Regex. Jane User wrote a blog post called ...