473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HtmlSpecialChar s()

I'm working on a modification to a popular blog script, the
modification is for putting source code into a post for the world to
see. The idea is exactly the same as putting code into a post on a
phpBB2 forum. When posting code it has to be between
Expand|Select|Wrap|Line Numbers
  1.  and
  2.  
. The text ends up in a nice looking table making for easy
reading.

The problem I have is that I want the text between
Expand|Select|Wrap|Line Numbers
  1.  and 
to have htmlspecialchar s() run on it before it is returned, however I
cannot figure out how to go about this.

The function currently looks like this:

// Function : CodeParse
// Description : Searches for
Expand|Select|Wrap|Line Numbers
  1.  and 
and replaces them
// Arguments : $input - The text to work on
// Returns : The replaced text
// Author : SoulSniper
// Last Change : 2004-15-19

function CodeParse($inpu t){

$before = array("
Expand|Select|Wrap|Line Numbers
  1. ", "
");
$after = array('</span><table width="90%" cellspacing="1"
cellpadding="3" border="0" align="center"> <tr><td><span
class="small">< b>Code/Quote:</b></span></td></tr><tr><td
class="code">', '</td></tr></table><span class="blog_bod y">');
$return = str_replace($be fore, $after, $input);

return $return;
}

Before anyone mentions I know the text in $after is not escaped, when
I escaped it all the \'s appeared in the resulting html code so when I
unescaped them it worked fine..

Help anyone?
Jul 17 '05 #1
3 2324
"SoulSniper " <ru*********@bl ueyonder.co.uk> wrote in message
news:cf******** *************** **@posting.goog le.com...
The problem I have is that I want the text between
Expand|Select|Wrap|Line Numbers
  1.  and 
to have htmlspecialchar s() run on it before it is returned, however I
cannot figure out how to go about this.


Bobo the Clown said it's regular expression time!

function __EscapeCode__( $m) {
return '</span><table width="90%" cellspacing="1"
cellpadding="3" border="0" align="center"> <tr><td><span
class="small">< b>Code/Quote:</b></span></td></tr><tr><td
class="code">'
. htmlspecialchar s($m[1])
. '</td></tr></table><span class="blog_bod y">';
}

function CodeParse($inpu t) {
error_reporting (E_ALL);
return preg_replace_ca llback('/\[CODE\](.*?)\[\/CODE\]/s',
'__EscapeCode__ ', $input);
}

Jul 17 '05 #2
"Chung Leong" <ch***********@ hotmail.com> wrote in message news:<Xd******* *************@c omcast.com>...
"SoulSniper " <ru*********@bl ueyonder.co.uk> wrote in message
news:cf******** *************** **@posting.goog le.com...
The problem I have is that I want the text between
Expand|Select|Wrap|Line Numbers
  1.  and 
to have htmlspecialchar s() run on it before it is returned, however I
cannot figure out how to go about this.


Bobo the Clown said it's regular expression time!

function __EscapeCode__( $m) {
return '</span><table width="90%" cellspacing="1"
cellpadding="3" border="0" align="center"> <tr><td><span
class="small">< b>Code/Quote:</b></span></td></tr><tr><td
class="code">'
. htmlspecialchar s($m[1])
. '</td></tr></table><span class="blog_bod y">';
}

function CodeParse($inpu t) {
error_reporting (E_ALL);
return preg_replace_ca llback('/\[CODE\](.*?)\[\/CODE\]/s',
'__EscapeCode__ ', $input);
}


Wow thanks!! It worked first time :)
Jul 17 '05 #3
You got to use Regular Expression!

function CodeParse($inpu t){
if(ereg('^(.*)\[CODE\](.*)\[\/CODE\](.*)$', $input, $arr)) {
$return = $arr[1].'</span><table width="90%" cellspacing="1"
cellpadding="3" border="0" align="center"> <tr><td>
<span class="small">< b>Code/Quote:</b></span></td>
</tr><tr><td class="code">'. htmlspecialchar s($arr[2]).'
</td></tr></table><span class="blog_bod y">'.$arr[3];
}
return $return;
}
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8031
by: Phil Powell | last post by:
If $val is the following: ....Just revamped the site's Content Management Application I built.. so do bear in mind.. sorry! Phil stripslashes(htmlspecialchars($val)) should produce the following, or so I thought:
0
2055
by: lawrence | last post by:
Using the conversion to char sets described for these functions, is it possible to get a whole string into some charset? I'm trying to figure out a way to take invalid character sets from idiot users, and translate the string so that in the end I have a string that won't cause my XML to test invalid. html_entity_decode() htmlspecialchars() htmlentities()
1
4230
by: leegold2 | last post by:
Newbie question I guess. Please show me how to use the htmlspecialchars function in the form below, Thanks: <form action="formtest1.php?c=1" method=POST> <b>Find Results with: These words: <input type="text" length=40 name="keywords"> input type="submit" value="Search"> </form>
1
2265
by: brianj | last post by:
Running php 4.3.6 on winxp machine I have following code: ----------------------------------------------------------------------- Restaurants <select size='1' name='restaurants'> <? while ( $row = mysql_fetch_array($restaurant)){ $opt = "<option value = '" . $row . "'>" . $row . "</option>"; $opt = htmlspecialchars($opt, ENT_QUOTES);
0
1687
by: Gandalf | last post by:
Hi all! I'm writting a web application using IIS and Python. I would like to have the Python equvalient of the PHP functions 'htmlentities' and 'htmlspecialchars'. E.g. to convert a' >>>> aacute; õ >>>> otilde; and the others. I looked for them in many libraries (urllib, htmllib, urlparse etc.) but I could only find the 'urlencode' function. I could not find anything on google ('python htmlentities equvalent'). Please
2
1857
by: universalbitmapper | last post by:
Hi, $new = htmlspecialchars("<a href=", ENT_QUOTES, 'ISO-8859-15'); echo $new; displays: <a href Instead of :
1
2129
by: Software Engineer | last post by:
Testing htmlspecialchars() PHP Function - Converting HTML Characters http://sqa.fyicenter.com/Online_Test_Tools/Test_htmlspecialchars_PHP_Function.php When data needs to be presented in Web pages, HTML special characters must be well protected Otherwise, data well not be presented properly, or the Web will become corrupted. This page allows you to learn and test the htmlspecialchars() function to protect embedded data in Web pages.
3
2905
by: DavidPr | last post by:
I have this when posting to a database: $a = htmlspecialchars($_POST); $a = addslashes($a); I have this when displaying the data on a Web page: $article=nl2br(stripslashes(mysql_result($result,$i,"article"))); The stripslashes works, but the html characters show up, i.e. < >, etc. htmlspecialchars going into the database is good, but how do I reverse it to display in a Web page?
8
7910
by: mijn naam | last post by:
Can someone please explain to me why/when one would use htmlspecialchars instead of htmlentities? I know: if you only want to get certain characters translated. This is not the answer I'm looking for, I would like to know *why* you would want that, as opposed to a full translation.
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.