Page 1 of 1

Fixing URLs

Posted: Sat Oct 09, 2010 2:52 am
by Cronje
I currently have a version of the MediaWiki software running locally on my computer. One minor thing I have issue with is that it converts things like [[link]]'s to <a href=".../Link">link</a>'s instead of <a href=".../Link">link's</a> (note the placement of the 's). In an effort to fix this, I decided to mess with the block of text that the wiki software spits out. The problem is, I can't get my REGEXP to work. Here's what I have so far. I'm using the $test variable to simulate the output code from the wiki.

Code: Select all

$test   = "<a href='test.php'>This is a test</a>'extra test";
$regex  = "<\/a>[a-zA-Z0-9-]*'[a-zA-Z0-9]*[^\.\?,!\s]";
What it's supposed to be doing is moving anything after the </a> (unless its punctuation or a space character) to before the </a>. What am I doing wrong?

I apologize if I gave a bit too much information and confused things.

Re: Fixing URLs

Posted: Sat Oct 09, 2010 8:16 am
by ridgerunner
This question was recently asked and answered. See: the Find all URLs to shorten for Twitter thread for a complete (but complex) solution).

The solution is complex because the problem is complex.

Re: Fixing URLs

Posted: Sat Oct 09, 2010 12:10 pm
by Cronje
What that script appears to do is find URLs in text and convert them to actual links. My problem is converting a link followed by text (specifically with that text containing an apostrophe) before a punctuation or space character. While I'm sure the code could be converted to fit my needs, my original post indicated that the REGEX is giving me trouble. Any further help would be appreciated.

Here's the code (plus a line I somehow left missing originally) again:

Code: Select all

$test   = "<a href='test.php'>This is a test</a>'extra test";
$regex  = "<\/a>[a-zA-Z0-9-]*'[a-zA-Z0-9]*[^\.\?,!\s]";

echo preg_replace("/$regex/","$0$1</a>",$test);

Re: Fixing URLs

Posted: Sat Oct 09, 2010 4:48 pm
by ridgerunner
Ok, if its just the case with apostrophe's, then that's pretty easy... Try this:

Code: Select all

$test   = "<a href='test.php'>This is a test</a>'extra test";
echo preg_replace('%</a>([\-\w]*\'\w*)%i', '$1</a>', $test);

Re: Fixing URLs

Posted: Sat Oct 09, 2010 7:03 pm
by Cronje
That's brilliant! Works like a charm. Thanks!