| Ultrashock Forums
• [PHP] nasty double quotes and single quotes... |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
2002-08-07
#2 |
||
|
|
|
2002-08-07
#3 |
||
|
er, i have to go to work in about..30 seconds but PCRE should work. Code:
$thetext = "go to ultrashock! or [LINK]http://www.identd.org/[/link]"; preg_replace("s/\[link=(.*?)\](.*?)\[\/LINK\]/gi", "<a href=\"$1\">$2</a>", $thetext); preg_replace("s/\[LINK\](.*?)\[\/LINK\]/gi", "<a href=\"$1\">$1</a>", $thetext; print $thetext."\n"; |
|
|
2002-08-07
#4 |
||
|
Originally posted by sexdwarf yes maybe i am just too much of a geek to think it's fun to try and convert regular expressions.......... ![]() but youre clearly not alone! snak9, parse error on line 3 .. no time to look at them now, either .. but i think its something to do with your quotes. |
|
|
2002-08-08
#5 |
||
|
gotta go to work in uhm less than 10 seconds ...line #3: Code:
preg_replace("s/\[LINK\](.*?)\[\/LINK\]/gi", "<a href=\"$1\">$1</a>", $thetext;
Code:
preg_replace("s/\[LINK\](.*?)\[\/LINK\]/gi", "<a href=\"$1\">$1</a>", $thetext);
|
|
|
|
2002-08-08
#7 |
||
|
though i'd post this up because it did fix my url problems ![]() $link = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([\/\w+\.]+)\b/i", "<a href=\"http$3://$4$5$6\" target=\"_blank\">$2$4$5$6</a>", $link); i tested it and and worked like a charm, it pick up anything beginning with http: or www. and prints it out as a link.. i can't understand it cause it's a little to advaced for me... can anybody explain it to me? what are $2, $3, $4, $5, $6? what are '\b' 's?' 'i'? i also haven't fixed the double quotes problem yet if anybody wants to contribute on that
|
|
|
|
2002-08-08
#8 |
||
|
Last edited by snaK9 : 2002-08-08 at 10:28.
tested this and i'm not sure how robust it is, but it seems to work.. Code:
<? $thetext = "go to ultrashock! or [LINK]http://www.identd.org/[/link]"; $thetext = preg_replace("/\[link=(.*?)\](.*?)\[\/link\]|\[link\](.*?)\[\/link\]/i", "<a href=\"$1$3\">$2$3</a>", $thetext); echo $thetext; ?> Code:
<? $quotes = "double quotes in my posts \"suck\"!"; echo stripslashes($quotes); ?> |
|
|
|
2002-08-08
#9 |
||
|
Last edited by snaK9 : 2002-08-08 at 11:17.
Originally posted by rufopr i'm not really too hot with pcre yet but i'll give it a shot.though i'd post this up because it did fix my url problems ![]() $link = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([\/\w+\.]+)\b/i", "<a href=\"http$3://$4$5$6\" target=\"_blank\">$2$4$5$6</a>", $link); i tested it and and worked like a charm, it pick up anything beginning with http: or www. and prints it out as a link.. i can't understand it cause it's a little to advaced for me... can anybody explain it to me? what are $2, $3, $4, $5, $6? what are '\b' 's?' 'i'? i also haven't fixed the double quotes problem yet if anybody wants to contribute on that
first the pattern is everything between the first and second /'s in the first parameter. so the actual pattern is: \b((http(s?):\/\/)|(www\.))([\w\.]+)([\/\w+\.]+)\b the i after the pattern is a modifer. i means case-insensitive. \b is a word boundary. so the \b at the beginning and at the end of the pattern means that it will match http://blah but not this is myurlhttp://blah () is a subpattern container. they can be nested as in this pattern. ((http(s?):\/\/)|(www\.))([\w\.]+)([\/\w+\.]+) there are three sub-patterns here. the first one is: ((http(s?):\/\/)|(www\.)) this sub-pattern contains two brances. the first branch matches http:// or https (? means 1 or 0, "s" may be present but may not). the | means OR and seperates branches. the second branch matches www. (the . has been escaped with a \ because it is a special character). would match www. in www.ultrashock.com the second sub-pattern ()([\w\.]+)([\/\w+\.]+)) matches any word character (\w means word character - that is any letter or digit or underscore) followed by a . (again escaped). [] means a class of characters, + indicates that there must be one or more. would match ultrashock. in www.ultrashock.com third sub-pattern is almost exactly the same as the second. the only difference is that / has been added to the character class. would match com in www.ultrashock.com the second parameter: "<a href=\"http$3://$4$5$6\" target=\"_blank\">$2$4$5$6</a>" replaces the pattern. references are made to the pattern matches. $0 is the whole pattern. $1 is the first match, $2 the second, etc. hope that helped a little bit. |
|
|
2002-08-08
#10 |
||
|
Wow, you get 100 points from me |
|
|
|
2002-08-08
#11 |
||
|
THANK YOU SNAK9! hahaha that was great... really helped a lot man! gonna try it out after work tomorrow... |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|



10 comments
| 306 views



like this
Linear Mode
1) are you trying to strip the slashes? in that case look into stripslashes
2) perl allways has and allways is my favorite, so when it comes down to things that will involve regular expressions in php i allways have a few problems because i am so used to the perl method...and as stated before, i am tired and need to go to bed, so instead of giving you the php answer, here is how you do it in perl, and you can try to convert it to php...if you can't i can convert it for you tomorrow:
and you can also just combine the two: