Need to get element from url - element

I have a url that looks like this
<span class="price" itemprop="lowPrice">14,70 €</span>
and i want to get the 14,70 € from this .
I have this
$url = "http://www.skroutz.gr/s/292655/Kingston-1GB-SODIMM-DDR3-Non-ECC-CL9-1333MHz.html";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$DOM = new DOMDocument();
#$DOM->loadHTML( $output);
//get all
$items = $DOM->getElementsByTagName('li');
//display all
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>";
but i get everything tagged with li . Is there a way just to get the 14,70 € ?
Thank you in advance for your help

Assuming you just want to extract the price from $span where $span =
<span class="price" itemprop="lowPrice">14,70 €</span>
A couple of preg_replace() calls will get it:
# remove span tag from beginning of string
$span = preg_replace('/^<span[^>]*>/', '', $span) ;
#remove span tag from end of string
$span = preg_replace('/<\/span.*$/', '', $span) ;

Related

How correctly to use the snippet() function?

My first Sphinx app almost works!
I successfully save path,title,content as attributes in index!
But I decided go to SphinxQL PDO from AP:
I found snippets() example thanks to barryhunter again but don't see how use it.
This is my working code, except snippets():
$conn = new PDO('mysql:host=ununtu;port=9306;charset=utf8', '', '');
if(isset($_GET['query']) and strlen($_GET['query']) > 1)
{
$query = $_GET['query'];
$sql= "SELECT * FROM `test1` WHERE MATCH('$query')";
foreach ($conn->query($sql) as $info) {
//snippet. don't works
$docs = array();
foreach () {
$docs[] = "'".mysql_real_escape_string(strip_tags($info['content']))."'";
}
$result = mysql_query("CALL SNIPPETS((".implode(',',$docs)."),'test1','" . mysql_real_escape_string($query) . "')",$conn);
$reply = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$reply[] = $row['snippet'];
}
// path, title out. works
$path = rawurlencode($info["path"]); $title = $info["title"];
$output = '<a href=' . $path . '>' . $title . '</a>'; $output = str_replace('%2F', '/', $output);
print( $output . "<br><br>");
}
}
I have got such structure from Sphinx index:
Array
(
[0] => Array
(
[id] => 244
[path] => DOC7000/zdorovie1.doc
[title] => zdorovie1.doc
[content] => Stuff content
I little bit confused with array of docs.
Also I don't see advice: "So its should be MUCH more efficient, to compile the documents and call buildExcepts just once.
But even more interesting, is as you sourcing the the text from a sphinx attribute, can use the SNIPPETS() sphinx function (in setSelect()!) in the main query. SO you dont have to receive the full text, just to send back to sphinx. ie sphinx will fetch the text from attribute internally. even more efficient!
"
Tell me please how I should change code for calling snippet() once for docs array, but output path (link), title for every doc.
Well because your data comes from sphinx, you can just use the SNIPPET() function (not CALL SNIPPETS()!)
$query = $conn->quote($_GET['query']);
$sql= "SELECT *,SNIPPET(content,$query) AS `snippet` FROM `test1` WHERE MATCH($query)";
foreach ($conn->query($sql) as $info) {
$path = rawurlencode($info["path"]); $title = $info["title"];
$output = '<a href=' . $path . '>' . $title . '</a>'; $output = str_replace('%2F', '/', $output);
print("$output<br>{$info['snippet']}<br><br>");
}
the highlighted text is right there in the main query, dont need to mess around with bundling the data back up to send to sphinx.
Also shows you should be escaping the raw query from user.
(the example you found does that, because the full text comes fom MySQL - not sphinx - so it has no option but to mess around sending data back and forth!)
Just for completeness, if REALLY want to use CALL SNIPPETS() would be something like
<?php
$query =$conn->quote($_GET['query']);
//make query request
$sql= "SELECT * FROM `test1` WHERE MATCH($query)";
$result = $conn->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
//build list of docs to send
$docs = array();
foreach ($rows as $info) {
$docs[] = $conn->quote(strip_tags($info['content']));
}
//make snippet reqest
$sql = "CALL SNIPPETS((".implode(',',$docs)."),'test1',$query)";
//decode reply
$reply = array();
foreach ($conn->query($sql) as $row) {
$reply[] = $row['snippet'];
}
//output results using $rows, and cross referencing with $reply
foreach ($rows as $idx => $info) {
// path, title out. works
$path = rawurlencode($info["path"]); $title = $info["title"];
$output = '<a href=' . $path . '>' . $title . '</a>'; $output = str_replace('%2F', '/', $output);
$snippet = $reply[$idx];
print("$output<br>$snippet<br><br>");
}
Shows putting the rows into an array, because need to lopp though the data TWICE. Once to 'bundle' up the docs array to send. Then again to acully display rules, when have $rows AND $reply both available.

How to use Sugarcrm\Sugarcrm\Util\Uuid::uuid1() in a custom entry point?

I am learning suitecrm. I need to create a new bean using a specific id from a custom entry point, generating the id is not working, when i try this code
// Create bean
$testAccountBean = BeanFactory::newBean('Accounts');
// Set the new flag
$testAccountBean->new_with_id = true;
$id = Sugarcrm\Sugarcrm\Util\Uuid::uuid1();
$testAccountBean->id = $id;
$testAccountBean->name = generateRandomString();
$testAccountBeanId = $testAccountBean->save();
echo $testAccountBeanId;
I get nothing
When I inspect the result of calling Sugarcrm\Sugarcrm\Util\Uuid::uuid1() nothing get in return.
Thanks for any idea
The function is called create_guid, require the include/utils.php and you will be able to call it.
<?php
if (!defined('sugarEntry')) {
define('sugarEntry', true);
}
require_once 'data/BeanFactory.php';
require_once 'include/utils.php';
$testAccountBean = BeanFactory::newBean('Accounts');
$id = create_guid();
Having said so - If you do $testAccountBean->new_with_id = true; it means you will provide your own ID, we use that to insert IDs from other systems/migrations. But if you need the GUID just remove that line and suitecrm will generate it for you.
You will need to call it in following manner:
$testAccountBean->new_with_id = true;
$testAccountBean->id = create_guid();
Note that if you assigned your own ID using create_guid function then "new_with_id" need to be set as well. You can find function at this path: include\utils.php
Following is the function body:
function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(' ', $microTime);
$dec_hex = dechex($a_dec * 1000000);
$sec_hex = dechex($a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = '';
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}

replace text with url in twitter feed

I have this code:
public function linkify($status_text)
{
$status_text = preg_replace('/(https?:\/\/\S+)/','\1', $status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
return $status_text;
}
and display feeds from twitter like this
foreach($feed as $feed_item) {
$html .= '<li>';
$html .= '' . $this->linkify($feed_item->text) . '';
$html .= '' . $this->relativedate((strtotime($feed_item->created_at))) . '';
$html .= '</li>';
}
echo $html;
result of this code is
<li>Twitter Feed Text http://t.co/TnkNfxCdRu</li>
if anyone can help me, how can I add text inside tag <a></a>. for example, to be exactly like this:
<li>Twitter Feed Text</li>
Thank you so much
Just change your first preg_replace to:
$status_text = preg_replace('~(https?://(\S+))~','$2',$status_text);

check if facebook URL redirected or not?

i want to check if the url's in my database are reaching the facebook page they should or redirected to "www.facebook.com".
this is the code i use:
<?php
$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
?>
<?php
$query = "SELECT data_txt FROM jos_sobi2_fields_data WHERE fieldid=8 ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$url = $row['data_txt'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach($row as $url) {
curl_setopt($ch, CURLOPT_URL, $url);
$out = curl_exec($ch);
$out = str_replace("\r", "", $out);
$headers_end = strpos($out, "\n\n");
if( $headers_end !== false ) {
$out = substr($out, 0, $headers_end);
}
$headers = explode("\n", $out);
foreach($headers as $header) {
if( substr($header, 0, 10) == "Location: " ) {
$target = substr($header, 10);
echo "[$url] redirects to [$target]<br>";
continue 2;
}
}
echo "[$url] does not redirect<br>";
}
?>
the result is this:
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
and this url -> http://www.facebook.com/common/browser.php is a facebook page that says my browser is old...probably because of some function in the code.....
anyway all i want to do is to check if the url in my database stays in their place with any redirection.
thanks :)
ronen.
Are you saying that you want to detect redirection, but the problem is you are always getting redirected to browser.php so you get nothing but "false positives"? In that case you probably just need to set the USERAGENT option, something like:
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');

xpath dom preg_match

I have this code with google map coordinates inside and i try to get coordinates but somewhere is a problem; The code is:
<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/>
and i try to get coordinates with this code:
$string = curl($e->textContent);
preg_match('#&ll=(.*?)&#is', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;
but dont work!
WHERE IS THE PROBLEM? WHERE I WRONG! (sorry for english)
This is my full code, but dont work:
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.kupoman.rs/aktivne-ponude/');
$xpath = new DOMXPath($dom);
$entries = $xpath->query("//div[#class='dealcontent']/h1/a/#href");
$output = array();
$i = 1;
foreach($entries as $e) {
$dom2 = new DOMDocument();
#$dom2->loadHTMLFile($e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$string = $xpath2->query("//iframe/#src")->item(0)->textContent;
$data['link']= ($e->textContent);
$data['naslov'] = trim($xpath2->query("//div[#class='dealcontent']/h1")->item(0)->textContent);
$data['slika'] = trim($xpath2->query("//div[#class='slideshow']/img[1]/#src")->item(0)->textContent);
preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;
all is good but coordinates is 0,0 :(
There's a lot wrong. There's no xpath expression, your regex is wrong and you are calling curl for some reason. Maybe you're trying to do something like this:
$dom = new DOMDocument();
#$dom->loadHTML('<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/>');
$xpath = new DOMXPath($dom);
$string = $xpath->query("//iframe/#src")->item(0)->textContent;
preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches);
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat;
$data['lng'] = $lng;