I have this string:
$str = 'সাংবাদিক<p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'<p>শাহজাদপুর </p>';
and I want to turn into:
$str = '<p>সাংবাদিক</p><p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg"><p>বিলুপ্ত হওয়া পাবনা</p>'
.'<p>বিলুপ্ত হওয়া পাবনা</p><img width="600" src="img/1.jpg">'
.'<p>বিলুপ্ত হওয়া পাবনা</p><img width="600" src="img/1.jpg"><p>বিলুপ্ত হওয়া পাবনা</p>'
.'<p>শাহজাদপুর </p>';
I tried regex
$str = preg_replace('/^(?!<p>).*(?!<\/p>)/m', '<p>$0</p>', $str);
but not doing properly. Please help
It isn't a job for regex but for DOMDocument. Since you are working with html parts and not a whole html document, you need to wrap your string into a basic html skeleton to avoid bad surprises with the auto-correction and to provide the document encoding:
$str = 'সাংবাদিক<p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'<p>শাহজাদপুর </p>';
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML('<html><head><meta charset="UTF-8" /></head><body>' . $str . '</body></html>');
$bodyNode = $dom->getElementsByTagName('body')->item(0);
$result = '';
foreach ($bodyNode->childNodes as $childNode) {
$result .= ($childNode->nodeType === XML_TEXT_NODE)
? '<p>' . $dom->saveHTML($childNode) . '</p>'
: $dom->saveHTML($childNode);
}
echo $result;
If I write a emoticon once more like " :) :) :) " its will replace only 1st one. Where is my problem here please.
My php
function smileys($text){
// Smiley to image
$smileys = array(
':)' => '<img src="smilies/smile.gif" border="0" alt="" />',
':-)' => '<img src="smilies/smile.gif" border="0" alt="" />',
':D' => '<img src="smilies/smile.gif" border="0" alt="" />',
':-(' => '<img src="smilies/angry.gif" border="0" alt="" />',
'angel' => '<img src="smilies/angel.gif" border="0" alt="" />',
);
// Now you need find and replace
foreach ($smileys as $search => $replace) {
$text = preg_replace("#(?<=\s|^)" . preg_quote($search) . "#", $replace, $text);
}
return $text;
}
echo''.smileys($description).'';
str_replace is the way to go.
function smileys($text)
{
$toReplace = array(
':)',
':-)',
':D',
':-(',
'angel');
$replacement = array(
'<img src="smilies/smile.gif" border="0" alt="" />',
'<img src="smilies/smile.gif" border="0" alt="" />',
'<img src="smilies/smile.gif" border="0" alt="" />',
'<img src="smilies/angry.gif" border="0" alt="" />',
'<img src="smilies/angel.gif" border="0" alt="" />');
return str_replace($toReplace,$replacement,$text);
}
This way you can replace all of the typed emotes to image tags, even different ones at once.
I had data in my table like this:
#inlcude<stdio.h>
main() {
printf("Hi");
}
But when I retrieve data from the database, it displays:
#inlude<stdio.h>main() { printf("Hi"); }
As you can see, there are no line breaks. Here is my entire code:
function BBCode ($str) {
$str = htmlentities($str);
$simple_search = array(
//added line break
'/\[br\]/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
//added textarea for code presentation
'/\[codearea\](.*?)\[\/codearea\]/is',
//added paragraph
'/\[p\](.*?)\[\/p\]/is',
//smilies
'/\:angry:/is',
'/\:angel:/is',
'/\:arrow:/is',
'/\:at:/is',
'/\:biggrin:/is',
'/\:blank:/is',
'/\:blush:/is',
'/\:confused:/is',
'/\:cool:/is',
'/\:dodgy:/is',
'/\:exclamation:/is',
'/\:heart:/is',
'/\:huh:/is',
'/\:lightbulb:/is',
'/\:my:/is',
'/\:rolleyes:/is',
'/\:sad:/is',
'/\:shy:/is',
'/\:sleepy:/is',
'/\:smile:/is',
'/\:tongue:/is',
'/\:undecided:/is',
'/\:wink:/is',
// For 3rd Party Code
'/\[code\](.*?)\[\/code\]/is',
'/\[php\](.*?)\[\/php\]/is',
'/\[css\](.*?)\[\/css\]/is',
'/\[jscript\](.*?)\[\/jscript\]/is',
'/\[sql\](.*?)\[\/sql\]/is',
'/\[perl\](.*?)\[\/perl\]/is',
);
$simple_replace = array(
//added line break
'<br />',
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
// added nofollow to prevent spam
'$2',
'$1',
'<div style="text-align: $1;">$2</div>',
//added alt attribute for validation
'<img src="$1" alt="" />',
'$2',
'$1',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
//added textarea for code presentation
'<textarea class="code_container" rows="20" cols="65">$1</textarea>',
//added paragraph
'<p>$1</p>',
//smilies
'<img src="comments/smilies/angry.gif" border="0" alt="" />',
'<img src="comments/smilies/angel.gif" border="0" alt="" />',
'<img src="comments/smilies/arrow.gif" border="0" alt="" />',
'<img src="comments/smilies/at.gif" border="0" alt="" />',
'<img src="comments/smilies/biggrin.gif" border="0" alt="" />',
'<img src="comments/smilies/blank.gif" border="0" alt="" />',
'<img src="comments/smilies/blush.gif" border="0" alt="" />',
'<img src="comments/smilies/confused.gif" border="0" alt="" />',
'<img src="comments/smilies/cool.gif" border="0" alt="" />',
'<img src="comments/smilies/dodgy.gif" border="0" alt="" />',
'<img src="comments/smilies/exclamation.gif" border="0" alt="" />',
'<img src="comments/smilies/heart.gif" border="0" alt="" />',
'<img src="comments/smilies/huh.gif" border="0" alt="" />',
'<img src="comments/smilies/lightbulb.gif" border="0" alt="" />',
'<img src="comments/smilies/my.gif" border="0" alt="" />',
'<img src="comments/smilies/rolleyes.gif" border="0" alt="" />',
'<img src="comments/smilies/sad.gif" border="0" alt="" />',
'<img src="comments/smilies/shy.gif" border="0" alt="" />',
'<img src="comments/smilies/sleepy.gif" border="0" alt="" />',
'<img src="comments/smilies/smile.gif" border="0" alt="" />',
'<img src="comments/smilies/tongue.gif" border="0" alt="" />',
'<img src="comments/smilies/undecided.gif" border="0" alt="" />',
'<img src="comments/smilies/wink.gif" border="0" alt="" />',
// For 3rd Party Code
'<pre class="brush: plain;">$1</pre>',
'<pre class="brush: php;">$1</pre>',
'<pre class="brush: css;">$1</pre>',
'<pre class="brush: jscript;">$1</pre>',
'<pre class="brush: sql;">$1</pre>',
'<pre class="brush: perl;">$1</pre>',
);
// Do simple BBCode's `
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
function getComments($tutid){
//fetch all comments from database where the tutorial number is the one you are asking for:
$results = mysql_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
//find the number of comments:
$commentNum = mysql_num_rows($results);
echo' <div class="post-bottom-section">
<h2> '.$commentNum.' comments so far (post your own)</h2>
<div class="primary">
<ol class="commentlist">';
$count = mysql_numrows($results);
$i = 0;
while ($i < $count){
$qazi_id = mysql_result($results,$i,"qazi_id");
$name = mysql_result($results,$i,"name");
$email = mysql_result($results,$i,"email");
$description =mysql_result($results,$i,"description");
$url = mysql_result($results,$i,"url");
$date = mysql_result($results,$i,"date");
echo' <li class="'.$css.'">
<div class="comment-info">
<img alt="" src="'.$grav_url.' class="avatar" height="42" width="42" />
<cite>
'.$name.' Says: <br />
<span class="comment-data">'.$date.'</span>
</cite>
</div>
<div class="comment-text">
<p>'.BBCode($description).'</p>
</div>
</li>';
$i++;
}
echo'</ol></div></div>';
}`
If I replace $description =mysql_result($results,$i,"description"); with $description =nl2br(mysql_result($results,$i,"description")); the code displays output as
#include<stdio.h><br /> main() <br /> { <br /%gt; printf("hi");<br /> }
<br /> tags appears.
Jest replace return $str;
with
return nl2br($str);
When a user wants to send a message, he can use emoticons.
What happens, is that a users clicks on an emoticon, so that it inserts the corresponding text, like this:
:D
Now, after the message has been sent, the other person wants to see it.
What I want is to replace the :D with an image...
Here is what I got:
$patterns = array();
$patterns[0] = '/:)/';
$patterns[1] = '/:(/';
$patterns[2] = '/:D/';
$patterns[3] = '/:C/';
$patterns[4] = '/:A/';
$patterns[5] = '/:H/';
$patterns[6] = '/:L/';
$patterns[7] = '/:O/';
$patterns[8] = '/:S/';
$patterns[9] = '/;)/';
$replacements = array();
$replacements[0] = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />';
$replacements[1] = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />';
$replacements[2] = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />';
$replacements[3] = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />';
$replacements[4] = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />';
$replacements[5] = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />';
$replacements[6] = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />';
$replacements[7] = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />';
$replacements[8] = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />';
$replacements[9] = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />';
preg_replace($patterns, $replacements, $bericht);
But these aren't the right Regexes...so I get a php error.
In the near future I'd like to take a tutorial on how regexes work, but I hope someone can help me out now.
Is there a simple and quick way to do this? Preferably per pattern like I do now, not in 1 big regex.
Since you don't need regex search patterns you can simply use str_replace.
From manual page:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace().
$bericht = str_replace($patterns, $replacements, $bericht);
Just escape the parenthesis:
$patterns[0] = '/:\)/';
$patterns[1] = '/:\(/';
$patterns[9] = '/;\)/';
and also, change the last line to:
$bericht = preg_replace($patterns, $replacements, $bericht);
You're replacing fixed strings, so there's absolutely no need to use regex patterns here at all; a straightforward str_replace() will do just fine.
Or better yet, use strtr(), because then you can use an associative array for your replacement data...
$replacements = array(
':)' = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />',
':(' = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />',
':D' = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />',
':C' = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />',
':A' = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />',
':H' = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />',
':L' = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />',
':O' = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />',
':S' = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />',
';)' = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />',
);
$output = strtr($input, $replacements);
Much simpler!
One thing you should watch out for is inadvertently replacing strings that aren't intended to be emoticons. This is quite a common problem for programs that do this kind of thing. (I've been caught out by Skype plenty of times converting things like (c) and 8) into icons when I didn't intend it to)
Im trying to write code which will add javascript in a variable. Everything is going find until i get the error "SyntaxError: missing ; before statement" ... I have found out that if i remove a line then it works (i will put // ---- on that line). Please can someone help?
$this->paint_javascript_function_text .= '
function image_'.$this->archive_id.'_rollover (){
document.getElementById("details").innerHTML = "'.$this->archive_name.'<br />'.$this->archive_dimensions.' - Price £'.$this->archive_price.' inc Packaging";
}
function image_'.$this->archive_id.'_fullsize (){
document.getElementById("gallery").innerHTML = ';
$this->paint_javascript_function_text .= "'";
$this->paint_javascript_function_text .= '<table width="100%" height="400"border="0" align="center"><tr><td align="right" valign="middle">';
if($this->archive_id == 1) {
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/prev_faded.png" border="0" class="paint_nav" />';
}
else
{
$prev = $this->archive_id - 1;
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_'.$prev.'_fullsize();" />';
}
$this->paint_javascript_function_text .= '</td><td width="400px" height="523px" align="center" valign="middle">'.$newimage.'<br />Back to Gallery</td><td align="left" valign="middle">';
if($total_num == $this->archive_id){
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/next_faded.png" border="0" class="paint_nav" />';
}
else
{
$next = $this->archive_id + 1;
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_'.$next.'_fullsize();" class="paint_nav" />';
}
$this->paint_javascript_function_text .= '</td></tr></table>';
// ---- $this->paint_javascript_function_text .= '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';
$this->paint_javascript_function_text .= "';
";
$this->paint_javascript_function_text .= '
$("#main_content").hide();
$("#gallery").show();
$("#paint_nav").hide();
}';
HTML Output:
function image_4_fullsize (){
document.getElementById("gallery").innerHTML = '<table width="100%" height="400" border="0" align="center"><tr><td align="right" valign="middle"><img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_3_fullsize();" /></td><td width="400px" height="523px" align="center" valign="middle"><img src="images/print_gallery/Countrymans Kitchen.jpg" /><br />Back to Gallery</td><td align="left" valign="middle"><img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_5_fullsize();" class="paint_nav" /></td></tr></table><center><span class="paintings_title">Countryman's Kitchen - - inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';
$("#main_content").hide();
$("#gallery").show();
$("#paint_nav").hide();
}
UPDATED
Change below line
$this->archive_name contains a single quote, so you need to escape that first with json_encode
$this->paint_javascript_function_text .= 'function image_'.$this->archive_id.'_rollover () {
document.getElementById("details").innerHTML = '.json_encode( $this->archive_name.'<br />'.$this->archive_dimensions.' - Price £'.$this->archive_price.' inc Packagin' ).';
}
function image_'.$this->archive_id.'_fullsize() {
document.getElementById("gallery").innerHTML = ';
You are concatenating using shorthand ".="
And then concatenating "the normal way".
try this way:
$this->paint_javascript_function_text = $this->paint_javascript_function_text . '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';