I have the following html content:
<p>My name is way2project</p>
Now I want this text as <p>My name is way2project</p>
Is there any way to do this? Please help me thanks
I used preg_replace but in vain.
Thanks again
You can use the strip tags function
$string = '<p>My name is way2project</p>';
echo strip_tags($string,'<p>');
note the second parameter is the list of allowed tags you wont to ignore.
This seems strange, but not knowing the complete scope of your issue and seeing that you want to do this in PHP, you can try:
$origstring = '<p>My name is way2project</p>';
$newstring = str_replace('way2project', 'way2project', $origstring);
echo $newstring;
Checkout Simple Html Dom Parser
$html = str_get_html('<html><body>Hello!SO</body></html>');
echo $html->find('a',0)->innertext; //prints "SO"
strip_tags you can use this, to remove html tags.
Related
i'm trying to extract "XXXXXXX" with PHP Simple HTML DOM description
<h2 class="title">XXXXXXX</h2>
I tried
$ret = $html->find('h2[class="title"]') ;
but i don't know the next instruction because there is no attribute. How i can do this ?
I need to extract also "XX" from this code, i think it's the same problem no ?
<a id="likeScore" appName='videos' object="video" objectid="96" direction="up" class="button like icon-heart youLike not-active">XX</a>
Thank you !
For the first one I think this could work:
$text = $html->find('h2[class="title"] a',0)->innertext;
For tags with ID you can use something more direct:
$text1 = $html->getElementById("likeScore")->innertext;
or using the #selector syntax
$text1 = $html->find('#likeScore',0)->innertext;
Documentation:
https://simplehtmldom.sourceforge.io/manual.htm#section_access
I'm customizing the layout of category blog view for my template and I need to display the article intro text without the tag. In my custom file "blogalternativelayout_item.php", I use:
<?php echo substr(($this->item->introtext),0,75); ?>
Anyway this renders the introtext as
<p>Lorem ipsum etc...</p>
How could I do to remove the paragraph tags?
Thanks in advance.
You can use php strip_tags() function. eg;
echo strip_tags($this->item->introtext);
the code above will strip all the html tags in introtext.
If you want to strip tags except tags, then you can put it like this:
echo strip_tags($this->item->introtext, "<a>");
You have to use regex to achieve this task
<?php
$text = substr(($this->item->introtext),0,75);
//get the contents inside <p> tag using this regex
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $text);
echo $result;
?>
Thanks to both suggestions. I've created this code and working:
<?php
$desctrunc = substr(($this->item->introtext),0,75);
$desc = strip_tags($desctrunc);
echo $desc . '...';
?>
Thanks.
I have a program which is copying the text from another website and showing it.
It is storing the text in a variable $string.
The variable is containing html tags in it and I want to add text before a html tag stored in the variable.
For example: $string="<div id='1'><div id='game'></div>"; I want to add text before the div whose id is game.
To add the text before the div whose id is 'game'. simply use:
$string = "<div id='1'><div id='game'></div>";
$new = "texttoinsert";
$pos = "<div id='game'></div>";
echo str_replace($pos, $new.$pos ,$string);
In php the easiest way to do this would be using str_replace (http://www.php.net/manual/en/function.str-replace.php).
$textToInsert = "test";
$string = str_replace("<div id='game'>", $textToInsert."<div id='game'>" ,$string);
For that particular case the following works:
$($string).prepend("text");
DEMO
Using jQuery the solution is simple:
var text = $("<div id='1'><div id='game'></div></div>");
$('#1', text).prepend('text-to-insert');
and the result HTML can be obtained like this: text.html()
I hope this help.
I want get some html code between 2 tag and I have 2 regex for it
1-$LinkGrabber = "<p><strong>item1:<\/strong> <span style=\"color: #ff0000;\"><strong>Full<\/strong><\/span><\/p>(.*)<p> <\/p>";
2-$linkGrabber = "<p><strong>item2<\/strong> <span style=\"color: #ff0000;\"><strong>Full<\/strong><\/span><\/p>(.*)<p> <\/p>";
first code work fine but second not.can you tel me what's different between these code?
I'd say, they both work fine but they're named different. Make sure, when testing the second one to use $linkGrabber instead of $LinkGrabber in the first example.
Don't ever use Regex to Parse HTML tags. Make use of a DOM Parser.
$dom = new DOMDocument;
#$dom->loadHTML($html); //<---- Pass your HTML source here
foreach ($dom->getElementsByTagName('p') as $tag) {
echo $tag->nodeValue; //"prints" the content of the p tag.
}
The first is looking for HTML tags that contains item1: while the second looks for item2...
I am doing something like posting function in a local app it's working fine really but it lacks with validation and not to mention the validation I made was a mess. I'm using jQuery oEmbed.
What I wanted is to print the illegal html tag(s) as is and activate/perform(I don't know the right term) the html tags I have allowed.
Any suggestions?
This is the best solution i came up.
First replaced all the < and > for html code then replaced back the allowed tags.
<?php
$original_str = "<html><b>test</b><strong>teste</strong></html>";
$allowed_tags = array("b", "strong");
$sans_tags = str_replace(array("<", ">"), array("<",">"), $original_str);
$regex = sprintf("~<(/)?(%s)>~", implode("|",$allowed_tags));
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags);
echo $with_allowed;
echo "\n";
Result:
guax#trantor:~$ php teste.php
<html><b>test</b><strong>teste</strong></html>
I wonder if there's any solution for replacing all at once. But it works.