preg-replace

question regarding php function preg_replace

Hi, I want to dynamically remove specific tags and their content from an html file and thought of using preg_replace but can't get the syntax right. Basically it should, for example, do something like : Replace everything between (and including) "" by nothing. Could anybody help me out on this please ? ...

Deleting portion of a text string in PHP

I've constructed a form, but some rows of the form can potentially be returned blank, with default values. I'm trying to find a way of searching the form output and then deleting the bit which I know is not needed - which looks like: <tr bgcolor="#FFFFFF"> <td>2E</td> <td id="8003">-800</td> </tr> I've used str_replace() effectively o...

PHP Regex remove unwanted data

Hi All I would like to be able to remove content from a string of data. This is an example string from google maps api. Distance: 70.5&#160;mi (about 1 hour 12 mins)<br/>Map data &#169;2009 Google I would like everything in between the brackets (). So can I remove everything from either side with preg_split ? Hope you can advise. ...

preg_replace out CSS comments?

I'm writing a quick preg_replace to strip comments from CSS. CSS comments usually have this syntax: /* Development Classes*/ /* Un-comment me for easy testing (will make it simpler to see errors) */ So I'm trying to kill everything between /* and */, like so: $pattern = "#/\*[^(\*/)]*\*/#"; $replace = ""; $v = preg_replace($pattern...

Including sample site into php and making it interactive

Hi there, I am working on a small project for learning PHP better. This project has a primary file, which opens another website (e.g. youtube) by using fopen + fread. (or file_get_contents ?) The site will be saved into a variable and after that a function will parse the file's content and should replace certain items with advanced con...

file_get_contents and file_put_contents with large files

Hi there! I'm trying to get file contents, replace some parts of it using regular expressions and preg_replace and save it to another file: $content = file_get_contents('file.txt', true); $content_replaced = preg_replace('/\[\/m\]{1}\s+(\{\{.*\}\})\s+[\x{4e00}-\x{9fa5}]+/u', 'replaced text', $contents); if ($content_replaced) { fi...

Regular expression matching multiple patterns

$url = "example-com--folder"; $searchArray = array('/-/','/--/'); $replaceArray = array('.','/'); $url = preg_replace($searchArray, $replaceArray, $url); The output I want is example.com/folder but all I get now is example.com..folder I know this is because I don't have the proper regex pattern, but what would that pattern be? ...

problem with preg_replace and str_replace

Hello again. $string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#",'<td><a href="' . "$front_page/" . str_replace(' ', '-', "$1") . '">'."$1</a></td>",$string); This part of script doesn't work: str_replace(' ', '-', "$1") I need to replace " " with "-", i also try preg_replace inside main preg_replace, str_ireplace also But this...

PHP preg_replace error

I have the following code: protected function safePath($path) { $path = (string) $path; $path = preg_replace( array( '#[\n\r\t\0]*#im', '#/(\.){1,}/#i', '#(\.){2,}#i', '#(\.){2,}#i', '#\('.DIRECTORY_SEPARATOR.'){2,}#i' ), array( '', '', '', '/' ), ...

preg_replace removes second backslash at start of line

Consider this: $sServerPath = "\\\\nlyehvedw1cl016\\projects$\\ARCLE_SW_SVN\\"; $sSVNParentPath = $sServerPath."svn\\"; $bla = " authz_module_name = TEST_TestRepos repository_dir = bla W"; $sSVNParentPath = $sServerPath."svn\\"; $sReplaceBy = "repository_dir = ".$sSVNParentPath.$sProjectName."\n"; echo $sReplaceBy; echo preg_replace ...

preg_replace problem

Hi, I'd like to match and extract variables from: {{variable:int}} variable would be anything a-z : is a separator int would be an integer 0-9 Curretly i have: preg_replace('!\{\{(\S+)\}\}!', "$1", $string) which does only half the job, i still have to split by :. Thank you! ...

PHP regular expression replace?

I want to split alpha-numeric (with space) and non-alpha-numeric by comma in a string. I tried with this... $str = "This is !@#$%^&"; preg_replace("/([a-z0-9_\s])([^a-z0-9_])/i", "$1, $2", $str); But I got this result... This, is, !@#$%^& How can I fix the search pattarn to get this result? This is, !@#$%^& Thanks. ...

Is there a MySQL equivalent of PHP's preg_replace?

I have a to match a field in MySQL, for which I thought I could use a regular expression, but it appears that MySQL doesn't have the functionality I need to do the job. Here's the scenario: I have a variable in PHP called $url. Let's say this variable is set as the string "/article/my-article/page/2". I also have a table of URLs in M...

preg_replace with function

I have some HTML that is being run through PHP: <a href="?char=">&</a> and I'm wanting to use a preg_replace to replace the first & with a urlencoded value of it. However: preg_replace("/char=\">(.*?)<\/a>/", "char=".urlencode("$1")."\">$1</a>", $links); But this gives me the value $1, instead of the expected back-reference. How ca...

how to return a regex match in php, instead of replacing

I'm trying to extract the first src attribute of an image in a block of HTML text like this: Lorem ipsum <img src="http://site.com/img.jpg" />consequat. I have no problem creating the regex to match the src attribute, but how do I return the first matched src attribute, instead of replacing it? From pouring over the PHP manual, it ...

Using preg_replace to format

Having trouble with pattern and replacement. How can I make the replacement echo a final product such as INSERT INTO `table` (`person`, `file`) VALUES ('test','test'), ('test2','test2'), ('test3','test3'); I am trying to insert the string into SQL but I need to format the current string below to do so, and I need to have the last part...

Check if a string is an email address in PHP

I am trying to do an SQL query, but I need to check somehow if the value is an email address. I need a way to check if $user is an email address, because I have user values such as this in my table. test test2 [email protected] [email protected] test392 [email protected] and so on... I need to make it so $useremail checks $user to find...

PHP Preg-Replace more than one underscore

How do I, using preg_replace, replace more than one underscore with just one underscore? ...

PHP not change color in quotes

I want to change the color of text inside single quotes, but i have also want to change the color of parenthasis as long as they arent in quotes. this is what i have but its not working as planned. "/([^'])\((.*?)\)([^']+)/is" => "$1<span style=\"color: ".$ccode['green']."\">(</span>$2<span style=\"color: ".$ccode['green']."\">)</span>$...

php getting part of a url into a sting

This question is more of a "what is the best/easiest way to do this type of question. I would like to grab just the users id from a string such as <a href="/profile.php?rdc332738&amp;id=123456&amp;refid=22">User name</a> I would like to parse the string and get just the "123456" part of it. I was thinking I could explode the string but...