Ultrashock Forums > Development > Server Side
[PHP] nasty double quotes and single quotes...

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
[PHP] nasty double quotes and single quotes...
Old 2002-08-07

i'm doing a simple form so i can post news up on my site. i wan't to know:

(1) when i post a message through the form that uses double quotes or single quotes they are, odviusly, printed out like this \"\" or \'\' i tried:
$post = ereg_replace("\\", "", $post); but didn't work
how can i correct this?

(2) how can i make php detect that i want something to be a url... for instance me writing on the form:
[LINK]http://whatever.com/[LINK] and it printing out:
<a href="http://whatever.com/">http://whatever.com/</a>

thanx guys

s m i l e *
postbit arrow 10 comments | 306 views postbit arrow Reply: with Quote   
Registered User
rufopr is offline
seperator
Posts: 12
2001-03-21
Age: 30
seperator

Ultrashock Member Comments:
sexdwarf sexdwarf is offline sexdwarf lives in United States 2002-08-07 #2 Old  
going to bed so my reply is short and sweet, maybe a little fluff and whip cream, but no cherries on top tonight...

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:
Code:
#!/usr/bin/perl
$thetext = "go to [LINK]http://www.ultrashock.com/[/link]";
$thetext =~ s/\[LINK\](.*?)\[\/LINK\]/<a href="$1">$1<\/a>/gi;
print $thetext."\n";
or you can extend the code to allow for link name by doing:
Code:
#!/usr/bin/perl
$thetext = "go to ultrashock!";
$thetext =~ s/\(.*?)\[\/LINK\]/<a href="$1">$2<\/a>/gi;
print $thetext."\n";


and you can also just combine the two:

Code:
";
$thetext =~ s/\[link=(.*?)\](.*?)\[\/LINK\]/<a href="$1">$2<\/a>/gi;
$thetext =~ s/\[LINK\](.*?)\[\/LINK\]/<a href="$1">$1<\/a>/gi;
print $thetext."\n";
hope this helps you...let me know if you need help with the php conversion...but it shouldn't be that hard...mgiht be fun for someone out there to do (or maybe i am just too much of a geek to think it's fun to try and convert regular expressions..........)
Reply With Quote  
snaK9 snaK9 is offline 2002-08-07 #3 Old  
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";
Reply With Quote  
x3ro x3ro is offline x3ro lives in Australia 2002-08-07 #4 Old  
Originally posted by sexdwarf
maybe i am just too much of a geek to think it's fun to try and convert regular expressions..........
yes






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.
Reply With Quote  
sexdwarf sexdwarf is offline sexdwarf lives in United States 2002-08-08 #5 Old  
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;
should be
Code:
preg_replace("s/\[LINK\](.*?)\[\/LINK\]/gi", "<a href=\"$1\">$1</a>", $thetext);
i believe...can't test, gotta run
Reply With Quote  
rufopr rufopr is offline 2002-08-08 #6 Old  
still nothing...
ok i tried what snak9 suggested (since i know nothing about perl)

after testing this is the error i get
what is s/ and /gi anyway? is that like ^ (beginning) and $ (end) on a ereg function?

oh and about the backslashes... stripslashes didn't work i've already tried it like this
$thetext = "double quotes in my posts \"suck\"!";
$thetext = stripslashes($thetext);
print $thetext."\n";
and just got this printed
double quotes in my posts \"suck\"!

i'd appretiate if someone could clarify things for me
Reply With Quote  
rufopr rufopr is offline 2002-08-08 #7 Old  
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
Reply With Quote  
snaK9 snaK9 is offline 2002-08-08 #8 Old  
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; 
?>
use stripslashes like so:

Code:
<?
$quotes = "double quotes in my posts \"suck\"!";
echo stripslashes($quotes);
?>
Reply With Quote  
snaK9 snaK9 is offline 2002-08-08 #9 Old  
Last edited by snaK9 : 2002-08-08 at 11:17.
Originally posted by rufopr
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
i'm not really too hot with pcre yet but i'll give it a shot.

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.
Reply With Quote  
poppa's Avatar poppa poppa is offline Moderator poppa lives in Sweden 2002-08-08 #10 Old  
Wow, you get 100 points from me
Reply With Quote  
rufopr rufopr is offline 2002-08-08 #11 Old  
THANK YOU SNAK9!

hahaha that was great... really helped a lot man!

gonna try it out after work tomorrow...
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: