preg-replace

Add Suffix To All Internal URLs

I need to add a suffix of ?hl=foo to the end of all internal URLs on my site. Im not sure of the best way to do this because of complications such as... <a href="http://www.example.com"&gt;My Site</a> <a target="_blank" href="http://www.example.com"&gt;My Site</a> <a class="a-class" href="http://www.example.com"&gt;My Site</a> ...

How to replace http:// or www with <a href.. in PHP

I've created this regex (www|http://)[^ ]+ that match every http://... or www.... but I dont know how to make preg_replace that would work, I've tried preg_replace('/((www|http://)[^ ]+)/', '<a href="\1">\1</a>', $str); but it doesn't work, the result is empty string. ...

wordpress wp_get_archives output change with preg_replace

good morning boys and girls...can someone point me to the right direction, please. i want to replace my php-echo-output »JUNE 29, 2009–JULY 5, 2009« with just plain text: »last week« <?php ob_start(); wp_get_archives('type=weekly&limit=1'); $wklyarchives = ob_get_contents(); ob_end_clean(); $wklyarchives = preg_replace('%\&\#8211\;[a-...

help with preg_replace

i want to replace html text that closed by tag start_ticker code.... end_ticker i don't success my code is $string_html = preg_replace('/<!-- start_ticker -->.*<!-- end_ticker -->/i',"bla bla",$string_html); ...

PHP preg_replace() compilation failed: missing )

Hello, I have the following function to return a clean path for a script. function cleanPath($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 - leaving in unwanted characters

I've got a string: $string = "Hello World!"; I want to turn it into a URL friendly tag, and I've developed a function to do it: function stripJunk($string){ $string = str_replace(" ", "-", $string); $string = preg_replace("/[^a-zA-Z]\s/", "", $string); $string = strtolower($string); return $string; } However, when I...

Replace URLs in text with HTML links

Here is a design though: For example is I put a link such as http://example.com in textarea. How do I get PHP to detect it’s a http:// link and then print it as print "<a href='htttp://example.com'>http://example.com&lt;/a&gt;"; I remember doing something like this before however, it was not fool proof it kept breaking for compl...

PHP preg_replace() backreferences used as arguments of another function

Hello. I am trying to extract information from a tags using a regex, then return a result based on various parts of the tag. preg_replace('/<(example )?(example2)+ \/>/', analyze(array($0, $1, $2)), $src); So I'm grabbing parts and passing it to the analyze() function. Once there, I want to do work based on the parts themselves: funct...

Replacing a string of text with another string using preg_replace

I have a string Action - [N]ew, [U]pdate, or [D]elete : N that I need to replace with "Action - [N]ew, [U]pdate, or [D]elete : U" somhow by using preg_replace I can't get it working. It remains the same. My code looks like this $action = Action - '[N]ew, [U]pdate, or [D]elete : U'; $line = preg_replace("/(Action - [N]ew, [U]pdate, or [...

I have HTML comments being wrapped in Li and P tags :(

I have content that is first htmlentities and then stripslashes followed by nl2br. This means a watermark at the end ends up as: <li><p><!-- watermark --></p></li> Not very useful. I have the code below to try and strip the html comments and stop it displaying but its not very good at it! $methodfinal = str_replace('<li><p><!--', '<...

PHP preg_replace problem

I've run into a hard problem to deal with. I am replacing a-tags and img-tags to fit my suggestions like this. So far so good. $search = array('|(<a\s*[^>]*href=[\'"]?)|', '|(<img\s*[^>]*src=[\'"]?)|'); $replace = array('\1proxy2.php?url=', '\1'.$url.'/'); $new_content = preg_replace($search, $replace, $content); Now my problem is tha...

PHP preg_replace() on multiple items

This is what I have so far: <?php $text = preg_replace('/((\*) (.*?)\n)+/', 'awesome_code_goes_here', $text); ?> I am successfully matching plain-text lists in the format of: * list item 1 * list item 2 I'd like to replace it with: <ul> <li>list item 1</li> <li>list item 2</li> </ul> I can't get my head around wrapping <ul> ...

Help with preg_replace, stop at non alpa_numeric character

I need some help improving this function I made for parsing the links in a Twitter. It creates links for hashtags and @replys. It all works fine, the problem is if a hashtag or @reply has a punctuation character at the end with no space, it gets added to the HREF URL. For example if I Tweeted "I really like #pizza, and #pop", the link f...

How can I replace a variable in a get query in PHP?

I have an URL http://test.com/test?xyz=27373&amp;page=4&amp;test=5 which I want to tranform by replacing the page=4 through page=XYZ how can I do that with preg_replace? ...

PCRE to replace #334455 hex with #345

I'm writing a function that replaces long hex coded color (#334455) with short one (#345). This can be only done when each color in hex is multiple of 17 (each hex pair consists of the same characters). e.g. #EEFFCC is replaced with #EFC, but #EDFFCC isn't replaced with anything. I want to make this with single preg_replace() call with...

PHP regular expression to remove tags in HTML document

Say I have the following text ..(content)............. <A HREF="http://foo.com/content" >blah blah blah </A> ...(continue content)... I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the URLs will all be different) Much thanks ...

PHP readdir and sort

I'm making a little gallery. I want to read the file names off a directory and print the file names below after I've stripped some leading numerals and file extensions. I have two versions of the code. Version 1 doesn't sort $current_dir = "$DOCUMENT_ROOT"."/weddings2/"; $dir = opendir($current_dir); // Open the sucker whil...

regex: match string only if not part of a tag

Hi, I am trying to match a string only if it is not part of an html tag. For example when searching for the string: "abc". <a href="foo.html">abc def</a> should match <p> foo bar foo abc foo bar</p> should match but <a href="abc.html">foo</a> should not match. Thanks for the help! ...

Regex to replace reg trademark

I need some help with regex: I got a html output and I need to wrap all the registration trademarks with a <sup></sup> I can not insert the <sup> tag in title and alt properties and obviously I don't need to wrap regs that are already superscripted. The following regex matches text that is not part of a HTML tag: (?<=^|>)[^><]+?(?=<|...

use preg_replace to replace character unless an escape character precedes

Hi all... i'm trying to do the following hope there's a reg_ex expert around to shed some light. I need to replace the character [ in my code and make it a {. But there is cases where the [ needs to remain a [ and not change. So the way i figured it is i need to use the preg_replace("[", "{", $string); function with a suitable regula...