Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 29th, 2011, 03:46 AM
mike224 mike224 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 1 mike224 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 20 sec
Reputation Power: 0
PHP Regex to remove BBCode [URL='websirte']

Hi there,

I'm quite new to regex and have been attempting to do this, but I'm not able to figure it out yet

Basically want to convert
PHP Code:
 a string [URL='anychars'sometext 
into
PHP Code:
 a string sometext 


I've already choped out the [/URL] from the string, since I use it to count the number of links, and stick the result into an array:
PHP Code:
 $sigLinksexplode("[/URL]"$signature);      
        
$numSigLinks count($sigLinks); 


now I'm trying to go through the sigLinks array and remove the remaining:
PHP Code:
[URL='whatever'



The PHP regex that I created (but doesnt work) was the following:

PHP Code:
 $str preg_replace('/[\[URL=\':.\/\-\d\w?\'\]]/'"",  $sigLinks[$i]); 


But this seemed to cut out the "sometext" string at the end

Idealy, I want to cut out the section
PHP Code:
[URL='[.*]'// where .* is an char of any length 

But I havent figured out how to do this yet

- Sorry, I'm still very new to regex and have been trying, but with very little success

Reply With Quote
  #2  
Old August 29th, 2011, 06:25 AM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,833 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 20 h 52 m 20 sec
Reputation Power: 4192
Welcome to DevShed Forums, mike224.

1) You put your entire expression inside the brackets. That tells the engine to match the characters in any order, not just the order you want.

You only want the brackets around the characters that will appear between the single-quotes:
Code:
'/\[URL=\'[:.\/\-\d\w\?]\'\]/'

2) The question mark is a special character, so when it's not in a character class (the characters between the square brackets), it needs to be escaped with a backslash like the other special characters.

3) You need to also put a quantifier after the character class, because by itself the character class will only match a single character. In this case you may want to use the "+" which means to match 1 or more characters.
Code:
'/\[URL=\'[:\.\/\-\d\w\?&=]+\'\]/'

However, I would recommend using a negative character class instead, so you don't have to specify every character that might appear in a URL. The "^" as the first character in a character class indicates that any character not in the class will be matched, so we want to match any character that is not a single quote:
Code:
'/\[URL=\'[^\']+\'\]/'

Useful references:
http://www.php.net/manual/en/pcre.pattern.php
http://www.regular-expressions.info/reference.html
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > PHP Regex to remove BBCode [URL='websirte']

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap