SitePoint Sponsor |
|
User Tag List
Results 1 to 22 of 22
-
Jan 14, 2001, 08:07 #1
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
is there some function in PHP that does exactly the opposite of htmlspecialchars()?
In Kev's tutorial, under chapter .. Contetn Formatting, is explained how to define custom "tags" for your cms. In the beginning he uses htmlspecialchars to get rid of those < and > etc.
But I thought it could be useful if there was a tagHTML Code:and
But to achieve this, I need to reverse < to < etc. And therefore I want a function like htmlregularchars or so.
Does that exist?
Thanks
Thomas
-
Jan 14, 2001, 08:37 #2
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
On PHP.net, there is this example..
(I modified it to preserve existing html)
// $document should contain an HTML document.
// This will remove HTML tags, javascript sections
// and white space. It will also convert some
// common HTML entities to their text equivalent.
$search = array ("'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as php
$replace = array ("\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$text = preg_replace ($search, $replace, $document);
-
Jan 14, 2001, 12:19 #3
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the code.
Is it possible to do it with ereg_replace() instead of preg_replace. What are the differences between the two forms of regular expressions?
Thomas
-
Jan 14, 2001, 12:36 #4
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
preg_replace allows you to use Perl style pattern matching and can take arrays as params.. you cant simply swap one for another.
-
Jan 14, 2001, 12:53 #5
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I just use:
$text = str_replace ("<", "<", $text);
$text = str_replace (">", ">", $text);
That is if you only need these two characters:
"<" and ">"
Hope this helps
-
Jan 14, 2001, 15:02 #6
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The method I will take depends on which ca-haractars htmlspecialchars() replaces.
Does someone has a list of that?
Thanks
Thomas
-
Jan 14, 2001, 15:37 #7
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
(taken from php.net)
"The translations performed are:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>' "
-
Jan 14, 2001, 16:17 #8
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, thanks.
That's all I need to know. I will take Pete's way, but with the other translated stuff too.
I'm not familiar with Perl.
Thanks everybody!
Btw, str_replace, is it exactly the same as ereg_replace()?
Thomas
-
Jan 14, 2001, 16:23 #9
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Manual:
This function replaces all occurences of needle in haystack with the given str. If you don't need fancy replacing rules, you should always use this function instead of ereg_replace().
And what is haystack? And needle?
</thomas>
-
Jan 14, 2001, 16:40 #10
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ever heard the phrase "needle in a haystack" ?
It simply means it looks for needle in haystack
The reason that preg.. is used is because it can accept an array as a parameter.. making it a lot simpler to replace a lot of things in one go.
If you are looking for speed, use..
str_replace ("<", "<", str_replace (">", ">", str_replace ("&", "&", $text) ) );
etc for each html entity..
-
Jan 15, 2001, 07:37 #11
- Join Date
- May 2000
- Location
- Canada
- Posts
- 533
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
preg is faster than ereg from my experience(i talk purely from what i see, never ran benchmarks)
---------------------------
myPHPhost.com
-
Jan 15, 2001, 13:16 #12
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, thanks everyone for helping me.
Now I'm faced with this:
There is a variable that contains a bunch of text.
$text = "blabla";
That variable is put into htmlspecialchars():
$text = htmlspecialchars($text);
In this variable will be special codes likeHTML Code:and
Now I need some code to find those html tags, and then replace the <, >, &, " and ' within those tags.
How should I do that?
Thomas
-
Jan 15, 2001, 13:49 #13
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I 'm not quite sure what you mean but i give it a go:
$text = str_replace ("<html>", "<html>", $text);
$text = str_replace ("</html>", "</html>", $text);
this replaces the <html> and </html> tags. They must be lower case to change them.
<Edited by petesmc on 01-15-2001 at 01:54 PM>
-
Jan 15, 2001, 14:14 #14
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What I mean:
In Kevin Yank's tutorial on PHP/MySql, chapter on Content Formatting, he explaines how to format your text using tags like [ .. ] (for example [ b ]) (I can't put them together, because otherwise the forum would think it's real)
Now I want to include something like that, but with [html] tags.
With the bold stuff, it's just [ b ] that is replaced with <B>. But with the HTML tags [html], that is inappropriate.
The html tags should disappear, and on the contents within it, the next transformations should be made:
$text = str_replace("&","&",$text);
$text = str_replace("<","<",$text);
$text = str_replace(">",">",$text);
$text = str_replace(""","\"",$text);
$text = str_replace("'","'",$text);
How should I do that?
<Edited by Jppr on 01-15-2001 at 02:17 PM>
-
Jan 15, 2001, 14:22 #15
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You mean:
$text = str_replace("\HTML Code:","<html>",$text); $text = str_replace("\
$text = str_replace("&","&",$text);
$text = str_replace("<","<",$text);
$text = str_replace(">",">",$text);
$text = str_replace(""","\"",$text);
$text = str_replace("'","'",$text);
I'm not sure if i understand...
-
Jan 15, 2001, 14:29 #16
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No, not really.
First the script should find the portions of the $text var, that are withinHTML Code:and
Then with that portions the transformations should be made, so that &,",',<,> are displayed as should be.
If I don't, those chars will be treated as regular text, while they should be pure html code.
See?
-
Jan 15, 2001, 14:36 #17
- Join Date
- Nov 2000
- Location
- Hong Kong
- Posts
- 1,508
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, now i understand but i don't know how to do that.
i am guessing that you would use an explode function but i'm not the person to ask, maybe Freddy and Chris ( TWTCommish)
Sorry about that...
-
Jan 15, 2001, 14:40 #18
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Nah, he wants to avoid converting the HTML between
HTML Code:and
I suppose you will want to avoid regexps again?
if so,
e.g.
$text = "<something>HTML Code:<b>html here!</b>
HTML Code:<b>and here!</b>
$something = explode("HTML Code:", $text); while( list($key, $val) = each($something)) { $somethingelse = explode("
$newarr[] = $somethingelse[0] . htmlspecialchars($somethingelse[1]);
}
$text = implode("", $newarr);
How ever, a regexp may in fact be just easier..
-
Jan 15, 2001, 14:44 #19
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I read your post again.. and I realised you may mean something different to what I thought..
If you want the text betweenHTML Code:and
change to this..
$newarr[] = htmlspecialchars($somethingelse[0]) . $somethingelse[1];
-
Jan 15, 2001, 16:22 #20
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey, do I speak Chinese or what
No, in the very beginning of the script, the whole $text var will be htmlspecialchars'ed. That's because of my custom tags.
Now, in one tag (the [html] one) I want to undo the htmlspecialchars(). I want exactly the opposite.
Normally, that wouldn't be the problem. But it's because these [html] portions are within a large text amount, that I have to use a regexp first to find those portions, and then, in these portions, I need to do the replacement of &, ", <, >, ".
See what I mean ?
How should I accomplish such a mix of stuff.
By the way, what d these implode and explode functions do?
Thanks a lot for all of your help, especially PeterW and petesmc.
-
Jan 16, 2001, 05:34 #21
- Join Date
- Jul 2000
- Location
- Warwickshire, England
- Posts
- 557
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's what my code does
$text = "HTML Code:<b>html here!</b>
HTML Code:<b>and here!</b>
// take $text as your text..
// everything
$something = explode("[html]", $text);
// explode where this aHTML Code:while( list($key, $val) = each($something)) { // for every block of [html] $somethingelse = explode("
// explode in to two parts, before and after [/html]
$newarr[] = $somethingelse[0] . htmlspecialchars($somethingelse[1]);
// the part outside the you do htmlspecialchars on..
}
$text = implode("", $newarr);
// join them back up...
This produces...
<b>html here!</b><not here> <b>and here!</b>
-
Jan 16, 2001, 14:15 #22
- Join Date
- Dec 2000
- Location
- Belgium
- Posts
- 132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, I didn't tested it yet, but it looks promising!
Brilliant code PeterW!
Another question: where should I put the code to convert other codes like [ b ] , and [ code ] and [ link ], etc.?
I suppose after the implode command, is that correct?
The code will be something like
Code:$text = ereg_replace("\[ b ]","<B>",$text); ... $text = ereg_replace("\[ link ]([-_./a-zA-Z0-9!&?%#,'=:~]+)\[/ link ]","<A HREF=\"\\1\">\\1</A>",$text);
PeterW, thanks a LOT!
Cheers
Thomas
Bookmarks