How to show record made by tinyMCE? - php

I use tinyMCE to input the blog content record. i write this:
This is my new entry
i format it with justify and heading 1
then i display it, but the result is:
<h1 style="text-align: center;">This is my new entry</h1>
how to show it correctly

Tinymce adds html tags to all the text that you enter in it. By default it adds <p> to all the text that you enter and save.You need to use the function strip_tags() in PHP to remove the html tags.
Example:
$a = "<h1 style="text-align: center;">This is my new entry</h1>"; //this data is have assigned for demo you can get it from any place
echo strip_tags($a);

$text = 'Test paragraph. Other text';
echo strip_tags($text);
echo "\n";
// Allow and
echo strip_tags($text, '');
//output :
//Test paragraph. Other text
//Test paragraph. Other text

Related

Find URL in string, replace it and alter href

I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!
This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>

Regular expression from wysiwyg

I have HTML-code, that come from user who use wysiwyg redactor.
I need to сlean code from tags like <b ..><i ..><strong><p><a ..>, and clean up from all main js code, like onclick and other.
Thanks.
Use strip_tags to remove html from text. Example below.
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";

Cutting text in between destroys the design

I'm currently having an issue with my website. I count like 150 words and then cut it for displaying as an intro text on my website but this produces an issue.
When we have something like this in the text:
<div>
////TEXT TEXT TEXT TEXT TEXT////
----> Reach 150 words here <------
////TEXT TEXT TEXT TEXT TEXT////
</div>
It will print this in the front-page:
<div>
////TEXT TEXT TEXT TEXT TEXT////
----> Reach 150 words here <------
and the unclosed <div> tag destroys the design as it is expected.
How can I overcome this issue? Can we like proccess unclosed tags and close them in the end?
Use php's strip_tags to remove the div from your copy, then add it back in afterwards.
For example;
<?php
$html = '<div>Content goes here</div>';
$stripped = strip_tags($html);
$excerpt_pos = strpos(' ', $stripped, 150);
?>
<div><?php echo substr($stripped, 0, $excerpt_pos); ?></div>

Simple HTML DOM Parser - Get all plaintex rather than text of certain element

I tried all the solutions posted on this question. Although it is similar to my question, it's solutions aren't working for me.
I am trying to get the plain text that is outside of <b> and it should be inside the <div id="maindiv>.
<div id=maindiv>
<b>I don't want this text</b>
I want this text
</div>
$part is the object that contains <div id="maindiv">.
Now I tried this:
$part->find('!b')->innertext;
The code above is not working. When I tried this
$part->plaintext;
it returned all of the plain text like this
I don't want this text I want this text
I read the official documentation, but I didn't find anything to resolve this:
Query:
$selector->query('//div[#id="maindiv"]/text()[2]')
Explanation:
// - selects nodes regardless of their position in tree
div - selects elements which node name is 'div'
[#id="maindiv"] - selects only those divs having the attribute id="maindiv"
/ - sets focus to the div element
text() - selects only text elements
[2] - selects the second text element (the first is whitespace)
Note! The actual position of the text element may depend on
your preserveWhitespace setting.
Manual: http://www.php.net/manual/de/class.domdocument.php#domdocument.props.preservewhitespace
Example:
$html = <<<EOF
<div id="maindiv">
<b>I dont want this text</b>
I want this text
</div>
EOF;
$doc = new DOMDocument();
$doc->loadHTML($html);
$selector = new DOMXpath($doc);
$node = $selector->query('//div[#id="maindiv"]/text()[2]')->item(0);
echo trim($node->nodeValue); // I want this text
remove the <b> first:
$part->find('b', 0)->outertext = '';
echo $part->innertext; // I want this text

Replace Links Location (href='...')

I would like to replace the link location (of anchor tag) of a page as follows.
Sample Input:
text text text <a href='http://test1.com/'> click </a> text text
other text <a class='links' href="gallery.html" title='Look at the gallery'> Gallery</a>
more text
Sample Output
text text text <a href='http://example.com/p.php?q=http://test1.com/'> click </a> text text
other text <a class='links' href="http://example.com/p.php?q=gallery.html" title='Look at the gallery'> Gallery</a>
more text
I hope I have make it clear. Anyway I am trying to do it with PHP and reg-ex. Would you please light me up with right.
Thank you
Sadi
Don't use regular expressions for parsing HTML.
Do use PHP's built-in XML parsing engine. It works quite well on your question (and answers the question to boot):
<?php
libxml_use_internal_errors(true); // ignore malformed HTML
$xml = new DOMDocument();
$xml->loadHTMLFile("http://stackoverflow.com/questions/3099187/replace-links-location-href");
foreach($xml->getElementsByTagName('a') as $link) {
$link->setAttribute('href', "http://www.google.com/?q=" . $link->getAttribute('href'));
}
echo $xml->saveHTML(); // output to browser, save to file, etc.
Try to use str_replace ();
$string = 'your text';
$newstring = str_replace ('href="', 'href="http://example.com/p.php?q=', $string);

Categories