preg-replace

Avoiding processing special preg characters in replacement string

When using preg_replace() in PHP with strings generated at runtime, one can protect special regex characters (such as '$' or '+') in the search string by using preg_quote(). But what's the correct way to handle this in the replacement string? Take this code for example: <?php $haystack = '...a bit of sample text...'; $replacement = '\\...

php regex - splitting strings - one output form

I'm wondering a good way of splitting strings by a | delimiter if the input strings could be of the following form: "foo, bar" "foo ,bar" "foo,bar" "foo , bar" "foo bar" "foo,,bar" So the only possible outputs strings are as: "foo|bar" "foo|bar|other|here" Independently of how many terms are within the input string. Any t...

preg_replace: how do I strip off all white space and &nbsp;?

Hi, how do I strip off all white space and &nbsp;? I have this as a input in a wrapper I create, [b] &nbsp; &nbsp; &nbsp; bold &nbsp; &nbsp; &nbsp; [/b] so before turning the text to bold, i want to strip off all white spaces and &nbsp, and turn it into [b]bold[/b], $this->content = preg_replace("/\[(.*?)\]\s\s+(.*?)\s\s+\[\/(.*?)\]...

How to change all non word characters and multiple spaces into ' ' and then all space to '-' in one preg_replace()

Hello All, I want to change image names on below conditions All non word characters turns into space and then all spaces turns into - means if my image name is : Hello My name is'Kh@n "Mr. .Khan " then it should be changed into Hello-My-name-is-kh-n-mr-Khan . i need to use below in two steps, $old_name =' ...

PHP preg_replace to remove MS Office style html comments

Im looking for a way to remove MS Office style html comments, and everything in between. These are of the form: <!--[if gte mso 10]> blablabla blablabla <![endif]--> or <!--[if gte mso 9]> blablabla blablabla <![endif]--> The problem i'm encountering is that I do not want to remove ALL comments from the html. Only these MSO styl...

PHP Preg_match: attempt to match a string where a substring does not exist in the string

Hi, I have been trying to get this regex work. Suppose to parse an URL, if the string '_skipThis' is found, don't match the string. Also backreferences are needed too. For example: String 1: a_particular_part_of_string/a/b/c/d/e/f Result: preg_match should return true Backreference: $1 -> a_particular_part_of_string, $2 -> /a/b/c/d/e/f ...

Replace UL tags with specific class

Hi, I'm trying to replace all ul tags with a level0 class, something like this: <ul> <li>Test <ul class="level0"> ... </ul> </li> </ul> would be processed to <ul> <li>Test</li> </ul> I tried $_menu = preg_replace('/<ul class="level0">(.*)<\/ul>/iU', "", $_menu); but it's not working,...

Special Characters Problem

When I display contents from the database, I get this: ��Some will have a job. Others will want one. They are my people, they are my clients and they are being denied their rights. This text had been entered by the user via textarea with tinyMCE. How can I replace special characters (using preg_replace()) from the sentence to ' ' excep...

preg_replace: remove tags

I have a lot of option tags. I would like to remove the tags and get only the values. This is the code: <?php $result = preg_replace('/<option value=\"\d+\" >([A-Za-z0-9]+)<\/option>/', '$1', $result); ?> I cannot use strip_tags, strip_tags output: id="pesq_marca" class="select164" size="1" onchange="exibeModelosSelectpesq_marca(thi...

using preg_match and preg_replace

Hello. I have two strings like: http://localhost/web/ and http://localhost/web/category/ which sometimes become: http://localhost/web/2/, http://localhost/web/3/ etc.... and http://localhost/web/category/2/, http://localhost/web/category/3/ etc... I want to make a verification and: If the link is http://localhost/web/ it remain...

Howto change some words into links in HTML?

Hello, I want to change words from list (in file or database) into links in HTML website. I used str_replace, but i have problem with replacing words, that are already in links auchor. eg. I have html like this: Lorem ipsum donor et simet <a>lorem ipsum</a> eta raoa talkesa z uta. An i want to replace all "ipsum" into links, but skip...

preg_replace and date

I want to remove all - and / characters in a date string. Can someone give me a hand? Here is what I have but it doesn't work. preg_replace('/','',$date); preg_replace('-','',$date); Also, is there a way to group these two expressions together so I don't have to have 2 preg_replaces? ...

preg_replace conditional replacement with different string

I know there's a if/then in the matching of regular expressions, but is there one in the replace? ie. I want to do in one regex "I have Foo Bars under $5 for sale" to be "Foo Bars~$5" "I have Foo Bars for sale" to become "Foo Bars" and NOT "Foo Bars~" I have an expression a bit like this at the moment: preg_replace("/(([A-Z]...

How do I remove content between X and Y using preg_replace?

What would the regular expression be for removing any content between a quote, and the directory "uploads/"? Using a regexpression generator, I get this: (?<=\=")[^]+?(?=uploads/) $block_data = preg_replace('/(?<=\=")[^]+?(?=uploads/)/g','',$block_data); But seems to be removing everything :( ...

PHP Regular Expression Text URL to HTML Link

I'm working on a project where I need to replace text urls anywhere from domain.com to www.domain.com to http(s)://www.domain.com and e-mail addresses to the proper html <a> tag. I was using a great solution in the past, but it used the now depreciated eregi_replace function. On top of that, the regular expression used for such function ...

preg_replace in my PHP script doesn't work

I've a user form where I take a phone number as input in one of my fields. I have two seperate RegEx statements checking on the input. First one is: preg_match('/^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$/', $phone); and it works great. It can identify many different formats i.e. 222-333-44...

PHP preg_replace not working

Hello there. okay so i got a script that removes "_" and replaces it with a space " " and it does not work i even used ereg_replace and eregi_replace none worked yet it works backwards( " ","_") thanks a lot here is the script: $namefixed = preg_replace("/_/", " ", $name); and even $namefixed = preg_replace("_", " ", $name); ...

PHP preg_replace to turn **xyz** to <b>xyz</b>

I decided to, for fun, make something similar to markdown. With my small experiences with Regular Expressions in the past, I know how extremely powerful they are, so they will be what I need. So, if I have this string: Hello **bold** world How can I use preg_replace to convert that to: Hello <b>bold</b> world I assume some...

php preg replace :: regex

Hi there, If I have a string that looks like my name is {your name here} and I am from {country}. I am trying to use preg_replace to remove the {content} so the string ends up as my name is and I am from . But I can not work out the regex for the pattern. Can someone please help me out? my name is and I am from . is simply a sample ...

How do I linkify Twitter usernames using PHP preg_replace?

I want to search the text property of my twitter status objects and swap out @username for <a href="http:/twitter.com/username">@username</a>. What I have tried so far looks like this: $pattern = '/([@]{1})([a-zA-Z0-9\_]+)/'; $replace = '<a href="http://twitter.com/\2"&gt;\1\2&lt;/a&gt;'; $new_string = preg_replace($pattern, $replace, $...