Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2004, 11:12 AM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with regular expressions

This code below is used for making urls in my forum clickable.. but i also want to have a [img][/img] function which doesnt work because if i write http://www.text.com/pic.jpg the code below as already turned it into: [img]<a href="http://www.text.com/pic.jpg">http://www.text.com/pic.jpg</a>[/img]

.. both my functions crashes with one and another. How can i modify $prefix so that it doesnt react to a url which has [img] infront of it?

URL Replacer
Code:
    $prefix = '(http|https|ftp|telnet|irc|news|gopher|file|wais)://';
    $pureUrl = '([[:alnum:]/\n+-=%&:_.~?]+[#[:alnum:]+]*)';
    $myReplacement = eregi_replace('([[:space:]]|^)(www)', '\\1http://\\2', $myReplacement);
    $myReplacement = eregi_replace($prefix . $pureUrl, '<a href="\\1://\\2" target="_blank">\\1://\\2</a>', $myReplacement);
 
Old July 15th, 2004, 02:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

hey there

to solve this problem, you should not use just eregi_replace() function, because here the [img][/img] will not work. instead use perl compatible regexp functions like: preg_match_all() or preg_replace().... you will have to have a further insight into this because i'm not too good with regular expressions. i suggest you check out www.php.net

this is how i see the solution (pseudo code)

1) scan for [img][/img],[link][/link] etc...
2) for each type, extract attributes like "src" for image and "href" for link and then call appropriate preg_replace() function which will convert [link][/link] to <a></a>, and so forth and so on....

this might sound little bit confusing but it's better than nothing :D

peace

www.campusgrind.com the college portal
 
Old July 19th, 2004, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

hey there

i found the solution to your problem.... to overcome these problems you had, here's what i came up with. and so, for custom link tags:

[link=http://www.anything.com]any thing here[/link]

write this:

$message = $_POST["message"]; //usually textarea sent thru post method
$message = preg_replace("/\[link=(.*?)\](.*?)\[\/link\]/s","<a href=\"\\1\" target=\"_blank\">\\2</a>",$message);

where $message is a textarea where the user inputs his custom tags or whatever.

for custom image tags:

http://www.anything.com

write this:

$message = preg_replace("/\[img\](.*?)\[\/img\]/s","<img src=\"\\1\" border=\"0\">",$message)


hope that helped you :D


www.campusgrind.com the college portal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
Regular Expressions The_Iceman ASP.NET 2.0 Basics 0 September 29th, 2006 05:06 AM
I need some help in regular expressions! marwaesmat Perl 2 March 7th, 2006 05:22 PM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM
Regular Expressions Dave Doknjas C# 1 August 9th, 2003 12:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.