preg-replace

Minify CSS using preg_replace

Hey folks, I'm trying to minify multiple CSS files using preg_replace. Actually, I'm only trying to remove any linebreaks/tabs and comments from the file. the following works for me: $regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}'); echo preg_replace($regex, '', file_get_contents($file)); But I'd like to do it in a single multiline r...

[PHP] preg_replace: reference object in replacement

Hi all, Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Somet...

RegEx string "preg_replace"

I need to do a "find and replace" on about 45k lines of a CSV file and then put this into a database. I figured I should be able to do this with PHP and preg_replace but can't seem to figure out the expression... The lines consist of one field and are all in the following format: "./1/024/9780310320241/SPSTANDARD.9780310320241.jpg" or...

Preg_replace solution for prepared statements

Hi Guys, I have a command class that abstracts almost all specific database functions (We have the exactly same application running on Mssql 2005 (using ODBC and the native mssql library), MySQL and Oracle. But sometimes we had some problems with our prepare method that, when executed, replaces all placeholders with their respective val...

Using $ variables in preg_replace in PHP

Ummm... how do I use variables in a call to preg_replace? This didn't work: foreach($numarray as $num => $text) { $patterns[] = '/<ces>(.*?)\+$num(.*?)<\/ces>/'; $replacements[] = '<ces>$1<$text/>$2</ces>'; } Yes, the $num is preceeded by a plus sign. Yes, I want to "tag the $num as <$text/>". ...

preg_replace() str_replace() apostrophe nightmare! - Drupal menu image replace

Can anyone help me decode why this doesnt work? $cssid = preg_replace("/'/", "", $cssid); Trying to strip the single quote marks from some html... Thanks! H EDIT This is the full function - it's designed to rebuild the Drupal menu using images, and it applies CSS classes to each item, allowing you to select the image you want. Str...

preg_replace easy for a pro

i want to find first select ... from and replace that only, following code replace all select..from in sql query, i just need for first select..from preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql); ...

Question about preg_replace in PHP

In PHP what is the difference between using \1 or $1 as $replacement in preg_replace()? They both work and seem to do the exact same thing, but I think I'm missing something here. ...

Anchor tags to plain text within content

I am trying to match <a> tags within my content and replace then with the link text followed by the url in square brackets for a print-version. The following example works if there is only the "href". If the <a> contains another attribute, it matches too much and doesn't return the desired result. How can I match the URL and the link ...

Regular expression to replace an <a> with respective <img>

I'm looking for a PHP preg_replace() solution find links to images and replace them with respective image tags. Find: <a href="http://www.domain.tld/any/valid/path/to/imagefile.ext"&gt;This will be ignored.</a> Replace with: <img src="http://www.domain.tld/any/valid/path/to/imagefile.ext" alt="imagefile" /> Where the protocol MUST...

Using preg_replace to trim variable in PHP

I want to extract text from $_SERVER['HTTP_REFERER'] let's say $_SERVER['HTTP_REFERER'] = http://www.google.com/search?source=ig&amp;hl=en&amp;rlz=&amp;q=something+i+am+looking+for&amp;aq=f&amp;oq=&amp;aqi= then I want $query equal "something+i+am+looking+for". I figure I can use pregreplace so I can say $query=preg_replace([some re...

Need help with simple preg_match/preg_replace regex in php

I'm trying to do a simple task of altering thousands of music video embed codes with a common with/height. For example, I have the following code: <object width="480px" height="407px" > <param name="allowFullScreen" value="true"/> <param name="wmode" value="transparent"/> <param name="movie" value="http://mediaservices.mys...

Simple preg_replace

I cant figure out preg_replace at all, it just looks chinese to me, anyway I just need to remove "&page-X" from a string if its there. X being a number of course, if anyone has a link to a useful preg_replace tutorial for beginners that would also be handy! ...

Wrapping a chunk of text with tags using PHP

I'm trying to allow a user (using Wordpress) to insert a jquery slideshow gallery (http://www.queness.com/resources/html/slideshow/jquery-slideshow.html) based on a faux tag. For example: [slideshow] <img src="url" /><br /> <img src="url" /> [!slideshow] Would produce something similar to <div id="gallery"> <a href="#"><...

help with a preg_replace regular-expression

Hello all, I'm really confused as to using preg_replace but slowly learning. I need help with the function: $str= preg_replace('#\W\d+#', '\W \d+', $str); The idea is that Im looking for numbers that have letters directly before them with no spaces, ie abc123. (NOT 'abc 123' and NOT '123abc') and how do I simply include a space or...

Quickest way to Count the amount of numbers in a string, plus how to test PHP performance?

I need the best way of finding how many numbers in a string, would I have to first remove everything but numbers and then strlen? Also how would I go about testing the performance of a any PHP script I have written, say for speed and performance under certain conditions? UPDATE say I had to inlcude ½ half numbers, its definetly preg t...

Another preg_replace question!

Ive got a string that are words bunched together and I need to seperate them, and every word that ends in 'A' should probably be on a new line, item onea second itema third I also need to check if the word ending in 'A' should actually end in 'A' like extra or sultana. item oneasecond itemand an extra item I have an array full of ...

How can I match the domain part of a URL in PHP?

I'm so bad at regexp, but I'm trying to get some/path/image.jpg out of http://somepage.com/some/...etc and trying this method: function removeDomain($string) { return preg_replace("/http:\/\/.*\//", "", $string); } It isn't working -- so far as I can tell it's just returning a blank string. How do I write this regexp? ...

How can I find all matches of <element>something</element> with a regex?

So let's say that I have: <any_html_element>maybe some whitespaces <br/>Some text</any_html_element> And I want to remove the first <br/> after <any_html_element>. How can I do that? Best Regards ...

preg_replace multiple problem

I am trying to replace multiple urls in a pagination element with a new url. The replacement works fine when there is one occurrence of the url i.e. prev and next, but it breaks on breaks on the Pagination Numbers. It brings the first and last number links together. How do I get the preg_replace function realize that there are multiple ...