i want to repeat special character or TAB.
i try with str_repeat(str, int)
//my function repeat
function tab($num){
return str_repeat(' ', $num);
}
//if I call
echo '<table>' . "\r\n";
echo tab(3) . '<tr>';
//the result
<table>
	<tr>;
I have tried several ways with single quote and double quote,
but the results is always wrong
Use \t for printing tab
function tab($num){
return str_repeat('\t', $num);
}
function tab($num){
return str_repeat(html_entity_decode(' '), $num);
}
If you want to indent source code of your page, you should use "\t" instead of special characters.
function tab($num){
return str_repeat('\t', $num);
}
Otherwise if you need to indent text on your page (which user would see), add after tabs.
<span> </span> some text
Anyway your should use MVC instead of such things.
Related
High,
I'm using this function
function special_chars_replace($string){
$result = preg_replace("/[&%\$##'\*:\/\"\[\]\{\};\(\)\|\\\=!\^\?`~.,\+-]/", "", $string);
return $result;
}
to delete all spaces in a css class name.
<?php echo special_chars_replace(strtolower(str_replace(" ","",$itemTags))); ?>
How do i preserve the first space before the name? So i can use it for a css class name. For example: class="tags tag01 tag02"
Just add the space before you echo the string:
<?php
echo " ".special_chars_replace(strtolower(str_replace(" ","",$itemTags)));
?>
You can use this
<?php echo implode(" ",explode(" ",$itemTags)); ?>
Regular expression is the most effective way to do this.
echo preg_replace(' +', ' ', $itemTags);
What this does is look for one or more spaces (that's what the + does), and replaces it with a single space.
Code typed from memory.
I am running a RST to php conversion and am using preg_match.
this is the rst i am trying to identify:
An example of the **Horizon Mapping** dialog box is shown below. A
summary of the main features is given below.
.. figure:: horizon_mapping_dialog_horizons_tab.png
**Horizon Mapping** dialog box, *Horizons* tab
Some of the input values to the **Horizon Mapping** job can be changed
during a Workflow using the internal programming language, IPL. For
details, refer to the *IPL User Guide*.
and I am using this regex:
$match = preg_match("/.. figure:: (.*?)(\n{2}[ ]{3}.*\n)/s", $text, &$result);
however it is returning as false.
here is a link of the expression working on regex
http://regex101.com/r/oB3fW7.
Are you sure that the line break is \n, is doubt, use \R:
$match = preg_match("/.. figure:: (.*?)(\R{2}[ ]{3}.*\R)/s", $text, &$result);
\R stands for either \n, \r and \r\n
My instinct would be to do some troubleshooting around the s flag as well as the $result variable passed by reference. To achieve the same without any interference from dots and the return variable, can you please try this regex:
..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R
In code, please try exactly like this:
$regex = "~..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R~";
if(preg_match($regex,$text,$m)) echo "Success! </br>";
Finally:
If this does not working, you might have a weird Unicode line break that php is not catching. To debug, for each character of your string, iterate through all the string's characters
Iterate: foreach(str_split($text) as $c) {
Print the character: echo $c . " value = "
Print the value from this function: . _uniord($c) . "<br />"; }
i want to know how to keep all whitespaces of a text area in php (for send to database), and then echo then back later. I want to do it like stackoverflow does, for codes, which is the best approach?
For now i using this:
$text = str_replace(' ', '&nbs p;', $text);
It keeps the ' ' whitespaces but i won't have tested it with mysql_real_escape and other "inject prevent" methods together.
For better understanding, i want to echo later from db something like:
function jack(){
var x = "blablabla";
}
Thanks for your time.
Code Blocks
If you're trying to just recreate code blocks like:
function test($param){
return TRUE;
}
Then you should be using <pre></pre> tags in your html:
<pre>
function test($param){
return TRUE;
}
</pre>
As plain html will only show one space even if multiple spaces/newlines/tabs are present. Inside of pre tags spaces will be shown as is.
At the moment your html will look something like this:
function test($param){
return TRUE;
}
Which I would suggest isn't desirable...
Escaping
When you use mysql_real_escape you will convert newlines to plain text \n or \r\n. This means that your code would output something like:
function test($param){\n return TRUE;\n}
OR
<pre>function test($param){\n return TRUE;\n}</pre>
To get around this you have to replace the \n or \r\n strings to newline characters.
Assuming that you're going to use pre tags:
echo preg_replace('#(\\\r\\\n|\\\n)#', "\n", $escapedString);
If you want to switch to html line breaks instead you'd have to switch "\n" to <br />. If this were the case you'd also want to switch out space characters with - I suggest using the pre tags.
try this, works excellently
$string = nl2br(str_replace(" ", " ", $string));
echo "$string";
I am taking input as comments in my website. where i want few html tags to allow like
<h2>, <h3>, so on. . .
and few to ban.
But i am also using a function which check the part of string and replace it with smilies
let us say '<3' for heart and ':D' for lol
When i use function sanitizeHTML() which is following
public function sanitizeHTML($inputHTML, $allowed_tags = array('<h2>', '<h3>', '<p>', '<br>', '<b>', '<i>', '<a>', '<ul>', '<li>', '<blockquote>', '<span>', '<code>', '<img>')) {
$_allowed_tags = implode('', $allowed_tags);
$inputHTML = strip_tags($inputHTML, $_allowed_tags);
return preg_replace('#<(.*?)>#ise', "'<' . $this->removeBadAttributes('\${1}1') . '>'", $inputHTML);
}
function removeBadAttributes($inputHTML) {
$bad_attributes = 'onerror|onmousemove|onmouseout|onmouseover|' . 'onkeypress|onkeydown|onkeyup|javascript:';
return stripslashes(preg_replace("#($bad_attributes)(\s*)(?==)#is", 'SANITIZED ', $inputHTML));
}
It remove bad attributes and allow only valid tags but when string like <3 for heart come this function remove the part of string after <3 .
Note :
The smilies code which do not have html special chars < or > sign work fine.
You're using PCRE to parse html, which is never a good idea. The expression <(.*?)> will match everything from < up to the next >. You need something more like <[^>]+>. However, that still has problems (and will capture <3). You could use a negative lookahead (<(?!3)[^>]+>) to handle that specific case, but there are a lot of other cases to consider. You may want to consider using a DOM parser instead.
I want to know how i use htmlentities •' for ' in my code ?
How to escape single quote
hows apostrophe work in IE
while($row = pg_fetch_array($result))
{
if($row[3]=="")
{
$vmobj_Array[$i]=$row[0]."***".$row[1]."***".$row[2];
}
else
{
$vmobj_Array[$i]=$row[0].' ( '.$row[3].' )'."***".$row[1]."***".$row[2];
}
$i++;
}
I think every question should have an answer, so I'm posting here as well. Feel free to accept the answer someone else posted there just now.
htmlentities($str, ENT_QUOTES); or htmlspecialchars($str, ENT_QUOTES); should do the trick where $str should be replaced by the variable or string you want to escape (e.g., $row[0]). If you just want to add it, all you need to do is add it: print "Here's an apostrophe '";