preg-replace

PHP preg_replace - Regular expression not including a phrase

$test = "<div><b><i>#uniquetag#</b></i></div> <div>Keep this</div>"; $test = preg_replace("/<div(.*)#uniquetag#(.*)<\/div>/i", "#uniquetag#", $test); I want the result to be $test = "#uniquetag# <div>Keep this</div>"; But it returns $test = "#uniquetag#"; I think I know why. (.*) is greedy and it extend the search till the end...

How to increment count in the replacement string when using preg_replace ?

I have this code : $count = 0; preg_replace('/test/', 'test'. $count, $content,-1,$count); For every replace, I obtain test0. I would like to get test0, test1, test2 etc.. ...

Remove with regex the end portion of a string

I'm trying to remove from a string everything start with / char, so if I have my_value/j/im<b*+èo[/h>e\ylo I'd like to remove the string /j/im<b*+èo[/h>e\ylo and return only my_value. I thought to use something with str_replace but I'm not a great regex programmer and I'm doing practise with php. function clean_value ($value) { r...

PHP - reduce multiple slashes to single slash

Hi, I have a regular expression that I use to reduce multiple slashes to single slashes. The purpose is to read a url that is previously converted to a human readable link using mod_rewrite in apache, like this : http://www.website.com/about/me This works : $uri = 'about//me'; $uri = preg_replace('#//+#', '/', $uri); echo $uri; // e...

Correct syntax for replacing multiple occurrences with PHP's preg_replace

I've gone over other questions from stack overflow with similar ideas but none seem to resemble closely enough what I'm trying to do. Seems simple, but I'm in a pickle. I'm trying to replace multiple occurrences of a line break (\n) with only one line break, so people won't be able to submit more than one line break at a time. Here's wh...

Replace Multiple "-" PHP

Possible Duplicate: PHP Preg-Replace more than one underscore Hi, I'm just wondering how I can replace 2 or more - signs in a string with just one in PHP. So like 1-2---3--4 would go to 1-2-3-4 Thanks :) ...

How do I remove all question marks and a minus signs using a regex?

I can get it to remove all the question marks with the code below: preg_replace('/(\?+)/', '', $string) No matter what I seem to do I can't get it to also remove all the minus signs as well. Everything I try just breaks the whole regex. ...

Regular expression, replace multiple slashes with only one

It seems like an easy problem to solve, but It's not as easy as it seems. I have this string in PHP: ////%postname%/ This is a URL and I never want more than one slash in a row. I never want to remove the slashes completely. This is how it should look like: /%postname%/ Because the structure could look different I need a clever pr...

php preg_replace \

Hi , really simple question how can I preg_replace the backslash charactor ? ...

PHP Regex to grab {tag}something{/tag}

Howdy, I'm trying to come=up with a regex string to use with the PHP preg functions (preg_match, etc.) and am stumped on this: How do you match this string?: {area-1}some text and maybe a <a href="http://google.com"&gt;link&lt;/a&gt;.{/area-1} I want to replace it with a different string using preg_replace. So far I've been able to...

How to get A-Z and 0-9 from a string?

Hi! How do I get "Lrumipsm1" from "Lörum ipsäm 1!"? So what I need is to only get a-z and 0-9 from a string, using php. ...

need help with Preg_replace Regular expressions

these word1 and word2 is in brackets want to remove the whole line depends on word2 [word1] something-line-text [word2] some text again want to replace some text with another depends on word2 [word1] something-line-text [word2] some text again into REPLCACEDTEXT something-line-text some text again some line text (something/...

preg replace all links

$text = "Clip - http://depositfiles.com/files/8b5560fne Mp3 - http://letitbit.net/download/4920.adaf494fbe2b15c34a4733f20/Madonna___The_Power_Of_Good_Bye.mp3.html Madonna - The power of goodbye Your heart is not open, so I must go The spell has been broken...I loved you so Freedom comes when you learn to let go Creation co...

PHP Regex Replace with Backreference modified by functions

I want to replace the class with the div text like this : This: <div class="grid-flags" >FOO</div> Becomes: <div class="iconFoo" ></div> So the class is changed to "icon". ucfirst(strtolower(FOO)) and the text is removed Test HTML <div class="grid-flags" >FOO</div> Pattern '/class=\"grid-flags\" \>(FOO|BAR|BAZ)/e' Replacement ...

Find matching brackets using regular expression in PHP

Assuming I have this string: "abc{def{ghi{jkl}mno{pqr}st}uvw}xyz" and I want to match this: "{def{ghi{jkl}mno{pqr}st}uvw}" what should my regular expression look like..? In other words, the match should start with "{" and end with "}", but it must have as many {'s as }'s in between. ...

PHP: Using PHP to syntax highlight all <code>s on a page... ($highlight_string() isn't an option)

Hi Stackers! I'm using CodeIgniter, alongside the highlight_code("$string"); (More info) function to provide syntax highlighting to a dynamic site. I want the users to be able to submit their own posts written in a BBCode-style format. I'm using NBBC PHP library for this. My problem is that nomatter how I do it I cannot get NBBC to syn...

remove multiple whitespaces in php

I'm getting $row['message'] from my mysql db and I need to remove all whitespaces like \n \t and so on. $row['message'] = 'This is a Text \n and so on \t Text text.'; should be formated to: $row['message'] = 'This is a Text and so on Text text.'; I tried $ro = preg_replace('/\s\s+/', ' ',$row['message']); echo $ro; but ...

split string after some chars with preg_match

I found this code which will match at most 300 chars, then break at the next nearest word-break: $var = 'This is a test text 1234567890 test check12.' # 44 chars preg_match('/^.{0,300}(?:.*?)\b/iu', $var, $matches); echo $matches[0]; 44 is lower than 300, so I expect the output to be the same like $var. But the output is: This i...

preg_replace all characters up to a certain one

Hi. I have a string &168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5& and i have to delete, say, this part &185601651932|mobile|3|120|1& (starting with amp and ending with amp) knowing only the first number up to vertical line (185601651932) so that in result i would have &168491968426|mobile|3|1...

Memory leak with preg_replace

I'm using the preg_replace function to replace accents in a string, I'm working with UTF-8. I have incurred in what seems to be a memory leak, but I can't isolate the root cause, my code is rather simple: preg_replace( array_keys($aToNoAccents), array_values($aToNoAccents), $sText ); where $aToNoAccent...