Exploding Song title - php

I am having some issues exploding this title Song Artist – Song Name I am using the following code and having not very much luck.
$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
print '<div class="song"> <div class="options"> <a class="play" href="'.$url.'" data-url="'.$url.'" data-title="'.$artist.'"> </a> <a class="download" href="'.$url.'"> </a> </div> <div class="info"> <a class="direct" href="'.$url.'"> <div class="artist">'.$artist.'</div> <div class="title">A Rainy Night In Harlem (Freestyle)</div> </a> </div> </div>';
It should look like this when I display.
But instead it returns something that looks like this.

I found something that might interest you:
Add echo htmlentities($title)."<br>"; under $title2=$title->plaintext;, IN YOUR ORIGINAL code. Like this
$title2 = $title->plaintext;
echo htmlentities($title)."<br>";
Gives me: (for example:)
<h2 itemprop="name">Chris Brown – You Make Me This Way (I Got You) (LQ)</h2>
No - but a –
That is why the explode didn't work. You might get away with exploding on –
Checked it over here, and it seems to work.
Sorry for all the edits, I had a hard time displaying – :-)

You may use trim to remove any unwanted whitespaces:
<?php
$title2 = $html2->find('header.section-header h2',0);
$links = $title2->plaintext;
$str = explode ("–", $links);
$artist = trim($str[0]);
$song = trim($str[1]);
?>
<div class="song">
<div class="options">
<a class="play" href="" data-url="" data-title=""></a>
<a class="download" href=""></a>
</div>
<div class="info">
<a class="direct" href="">
<div class="artist"><?php echo $artist;?></div>
<div class="title"><?php echo $song;?></div>
</a>
</div>
</div>

Related

Getting the title of post

I am trying to get the title of a post using simple_html_dom the html roots can be seen below the part I am trying to get is titled This Is Our Title.
<div id="content">
<div id="section">
<div id="sectionleft">
<p>
Latest News
</p>
<ul class="cont news">
<li>
<div style="padding: 1px;">
<a href="http://www.example.com">
<img src="http://www.example.com/our-image.png" width="128" height="96" alt="">
</a>
</div>
<a href="http://www.example.com" class="name">
This is our title
</a>
<i class="info">added: Dec 16, 2015</i>
</li>
</ul>
</div>
</div>
</div>
Currently I have this
$page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : '';
$html = file_get_html('http://www.example.com/'.$page);
foreach($html->find('div#section ul.cont li div a') as $element)
{
print '<br><br>';
echo $url = 'http://www.example.com/'.$element->href;
$html2 = file_get_html($url);
print '<br>';
$image = $html2->find('meta[property=og:image]',0);
print $image = $image->content;
print '<br>';
$title = $html2->find('#sectionleft ul.cont news li a.name',0);
print $title = $title->plaintext;
print '<br>';
}
The issue is here $title = $html2->find('#sectionleft ul.cont news li a.name',0); I assume I am using the wrong selector but I am literally not sure what I am doing wrong..
ul.cont news means "find <news> elements that are a child of ul.cont".
You actually want:
#sectionleft ul.cont.news li a.name
EDIT: For some reason, it seems simple_html_dom doesn't like ul.cont.news even though it's a valid CSS selector.
You can try
#sectionleft ul[class="cont news"] li a.name
which should work as long as the classes are in that order.
If this seems a little hacky, forgive me, but... you can always employ PHP to run a quick .js:
<?php
echo '<script>';
echo 'var postTitle = document.querySelector("ul.cont.news a.name").innerHTML;';
if (!isset($_GET['posttitle'])) {
echo 'window.location.href = window.location.href + "?posttitle=" + postTitle';}
echo '</script>';
$postTitle = $_GET['posttitle'];
?>

Php to auto populate grids

I have the following html code:
<div class="media row-fluid">
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/1.png" alt="" />
</div>
<div class="item-info">
Title 1
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/2.png" alt="" />
</div>
<div class="item-info">
This is another title
<p>Some info and details go here.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
</div>
Which basically alternates between a span class with the widget class, and then the widget class without the span3 class.
What I wanted to know was if there was a way to have php "echo" or populate the details for and details under the "item-info" class. Would I need to use a foreach statement to get this done? I would be storing the information in a mysql database, and while I can get it to fill in the info one by one (repeatedly entering the and echoing out each image and item title) it's not practical when the content needed to be displayed is over 15 different items. I'm not well versed in foreach statements so I could definitely use some help on it.
If someone could help me perhaps structure a php script so that it can automatically output the html based on the number individual items in the database, that'd be greatly appreciated!
I'm wondering if the html + php (not including the foreach) would look like this:
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/<? $file ?>" alt="" />
</div>
<div class="item-info">
<?$title?>
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
EDIT:
I wanted to add some more information. The items populated would be based on a type of subscription - which will be managed by a group id.
I was initially going to use <? (if $_SESSION['group_id']==1)>
echo <div class="item-info">
$title
<p>$info</p>
</div>
so that only the subscribed items would populate. But, I would need it to iterate through all the items for group1 table and list it. Currently I know that I can do
<? (if $_SESSION['group_id']==1)
while ($row=mysql_fetch_assoc($sqlItem))
{
$itemInfo = $row['info'];
$image = $row['image'];
$title = $row['title'];
$url = $row['url'];
};
>
$sqlItem for now can only be assigned one thing (manually - as in: $sqlItem = '123'), unless I iterate through which is what I'm trying to figure out.
Just read that 'mysql_fetch_assoc' is being depreciated with 5.5, here is the new way and looks better, easier I think.. Hope this helps, was updated today.
I hope this helps http://php.net/manual/en/mysqli-stmt.fetch.php
replace the printf with echo '//then your html stuff
This will iterate through the rows in your database until their are no more matching records.
shouldn't a while be enough? It depends on the structure of your database and website (we didn't need so much HTML I think. Some more PHP maybe). Hope this helps.

How to search html using file_get_content() and retrieve a specific DIV class?

Good day.
I want to file_get_content() to load a webpage and use strip_tags() to get the string: Category:Apple.
<div class="category">
<span style="font-size:11px; font-weight:bold;">Category:</span>
<a href="/listing/A/new/yp/search.do?applicationInd=A"
class="category">Apple
</a>
</div>
eg.
$text = '<div class="category">
<span style="font-size:11px; fontweight:bold;">
Category:</span><a href="/listing/A/new/yp/search.do?applicationInd=A"
class="category">Apple</a></div>';
echo strip_tags($text); //Category:Apple
What php statement do I need to do to pass variable to $text for that ... ?
Use preg_match?
Try this
preg_match('|<a href="([^"]*?)" class="category">([^>]*?)<\/a>|smi', $text, $matches);
$cat = "Category:" . $matches[2];
echo $cat;

PHP preg_replace to insert string inside an existing string

Unfortunately I really cannot get my head around regular expressions so my last resort is to ask the help of you fine people.
I have this existing code:
<li id="id-21" class="listClass" data-author="newbie">
<div class="someDiv">
<span class="spanClass">Some content</span>
</div>
<div class="controls faint">
Link 2
Link 3
</div>
</li>
Due to a number of reasons, I have to use preg_replace to inject an additional piece of code:
Link 1
I think you can guess where that should go, but for the sake of clarity, my desire is for the resulting string to look like:
<li id="id-21" class="listClass" data-author="newbie">
<div class="someDiv">
<span class="spanClass">Some content</span>
</div>
<div class="controls faint">
Link 1
Link 2
Link 3
</div>
</li>
Can anyone help me with the appropriate regular expression to achieve this?
try this
$html = '<li id="id-21" class="listClass" data-author="newbie">
<div class="someDiv">
<span class="spanClass">Some content</span>
</div>
<div class="controls faint">
Link 2
Link 3
</div>
</li>';
$eleName = 'a';
$eleAttr = 'href';
$eleAttrValue = 'link2';
$addBefore = 'Link 1';
$result = regexAddBefore($html, $eleName, $eleAttr, $eleAttrValue, $addBefore);
var_dump($result);
function regexAddBefore($subject, $eleName, $eleAttr, $eleAttrValue, $addBefore){
$regex = "/(<\s*".$eleName."[^>]*".$eleAttr."\s*=\s*(\"|\')?\s*".$eleAttrValue."\s*(\"|\')?[^>]*>)/s";
$replace = $addBefore."\r\n$1";
$subject = preg_replace($regex, $replace, $subject);
return $subject;
}
I can suggest two things (Although I couldn't understand your problem clearly)
$newStr = preg_replace ('/<[^>]*>/', ' ', $htmlText);
this will remove all the html tags from the string. I don't know if it will be usefull for you.
Another recommendation would be to use strip_tags function. The second parameter of strip_tags is optional. You can define the tags you want to keep with the help of 2nd parameter.
$str = '<li id="id-21" class="listClass" data-author="newbie">
<div class="someDiv">
<span class="spanClass">Some content</span>
</div>
<div class="controls faint">
Link 2
Link 3
</div>
</li>';
echo strip_tags ($str,'<a>');
This will give you an output just with the links and whatever text in the html string.
Sorry if this also doesn't help.

issue with While statement

So I am trying to pull HEADLINES.. that I have saved in a DB, they all have unique IDs.. I need a way to be able to call them but type or ID for instance.
$stmt = $con->prepare("SELECT * FROM news WHERE type = 'sports'");
$stmt->execute();
while($rfr=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<div class="row">
<div class="col-lg-4">
<img src="<?php echo $rfr['folder'] . $rfr1['file'];?>.jpg" >
<h2><?php echo $rfr['headline'];?></h2>
<p>V.A. conducts study to determine effectiveness</p>
<p><a class="btn btn-primary" href="#" role="button">Read More »</a></p>
</div>
<div class="col-lg-4">
<img src="imgs/news/n4.jpg">
<h2><?php echo $rfr['headline'];?></h2>
<p>The Rev. Daniel Berrigan, a Roman Catholic priest and peace activist who was imprisoned for burning draft files in a protest against the Vietnam War, died Saturday. He was 94.</p>
<p><a class="btn btn-primary" href="#" role="button">Read More »</a></p>
</div>
<div class="col-lg-4">
<img src="imgs/news/n1.jpg">
<h2><?php echo $rfr['headline'];?></h2>
<p>President Barack Obama is getting one more chance to poke fun at fellow politicians, the press and himself at the annual White House Correspondents' Dinner.</p>
<p><a class="btn btn-primary" href="#" role="button">Read More »</a></p>
</div>
<?php
}
?>
Every time I run this code though.. It just enters only the headline from ID 1 everytime. and it makes a ton of duplicate post..
I want it to select through all the headlines in that type.. thats why im using the WHILE()
Please try this:
$stmt = $con->prepare("SELECT * FROM news WHERE type = 'sports'");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $rfr) {
?>
<div class="row">
<div class="col-lg-4">
<img src="<?php echo $rfr['folder'] . $rfr1['file'];?>.jpg" >
<h2><?php echo $rfr['headline'];?></h2>
<p>V.A. conducts study to determine effectiveness</p>
<p><a class="btn btn-primary" href="#" role="button">Read More »</a></p>
</div>
...
</div>
<?php } ?>
Why don't you just use a foreach? That way you don't have to worry about handling iterations and possible infinite loops that while loops have.
Also, I personally prefer not to make function calls and assignments in a while/foreach.. It's not that big of a deal to add 1 extra line just before the declaration. Will improve readability and make it easier to debug your code.

Categories