Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have string like this:
My code is:
$html = new DOMDocument();
$html->loadHTML($message);
$items = $html->getElementsByTagName('div');
foreach($items as $item) {
$headline = array();
if($item->childNodes->length) {
foreach($item->childNodes as $i) {
$headline[$i->nodeName] = $i->nodeValue;
}
}
$headlines[] = $headline;
}
foreach ($headlines as $key => $value) {
$quote = $value['blockquote'];
}.
print_r($quote);
output ="this is your message.
Your Ticket ID = :68
Response ID = :45check that if its not contains any error."
I want to get the Ticket ID =:68 and Response id =:45.
when i print the blockquote i get the above text.now i want to get my ticket id and response id but don't know how ?
You could achieve this with regular expressions
$var ="this is your message.
Your Ticket ID = :68
Response ID = :45check that if its not contains any error.";
$match = array();
$ticketID = '';
$responseID = '';
preg_match('/Your Ticket ID = :([0-9]+)/', $var, $match);
if(count($match) > 0) {
$ticketID = $match[0];
}
preg_match('/Response ID = :([0-9]+)/', $var, $match);
if(count($match) > 0) {
$responseID = $match[0];
}
var_dump($ticketID, $responseID);
Outputs:
string 'Your Ticket ID = :68' (length=20)
string 'Response ID = :45' (length=17)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to search MySQL table and get in result in which don't have . (dot). example is below
example1.com
example2.com
example3
example4 com
So i want return, example 3 and 4. So basically i want row which don't have . (dot).
You can just use NOT LIKE:
SELECT column
FROM tablename
WHERE column NOT LIKE '%.%'
You can do that #fthiella suggested, or you can filter your search results in this was, using the stristr PHP function:
<?php
$searchQuery = 'example';
$sql = 'SELECT * FROM yourTable WHERE domain LIKE "%' . mysql_real_escape_string($searchQuery) . '%"';
$query = mysql_query($query);
$total = mysql_num_rows($query);
if($total) {
$data = array();
while($row = mysql_fetch_array($query)) {
if(stristr($row['domain'], '.') !== false) {
$data[] = $row['domain'];
}
}
die('<pre>' . print_r($data, true) . '</pre>');
} else {
die('Nothing was found.');
}
Note that I don't know your database table structure, so this is a psuedo-example.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know php at all so sorry for a simple question - I've got the project started by another person so now I'm trying to finish it.
The problem is - I'm making an android app for which I can't make a change without changing a php and this language I don't know.
here's this part :
function getPlaces(){
$result = array();
$sql = "SELECT id, name, short_description, photo_list, selected, recommended, isTOP FROM place
WHERE id IN(SELECT id_place FROM rubric_place WHERE id_rubric IN(SELECT id FROM rubric WHERE name = '".$_REQUEST["rubric"]."')) ORDER BY isTOP DESC";
getConnect();
$query = mysql_query($sql);
if(!$query){
error100();
}else {
$result['code'] = 200;
for ($i = 0; $i < mysql_num_rows($query); $i++) {
$row = mysql_fetch_assoc($query);
$result["places"][$i] = $row;
$sql = "SELECT * FROM rubric WHERE id IN(SELECT id_rubric FROM rubric_place WHERE id_place = ".$row[id].")";
$queryModule = mysql_query($sql);
if($queryModule){
for ($k = 0; $k < mysql_num_rows($queryModule); $k++) {
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rubrics"][$k] = $rowModule;
}
}
$sql = "SELECT SUM(rating)/COUNT(rating) AS rating FROM comment WHERE id_place = ".$row[id];
$queryModule = mysql_query($sql);
if($queryModule){
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rating"] = $rowModule[rating];
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
}
what I need is to make another function that returns in $result a value of "length" of places. I know I can find out length on the other end - in Android's app result, but this particular function will be changed so it will return by 20 results only so I need another function that returns length so plz help
To count the number of characters in a json you can do:
$string = json_encode($result, JSON_UNESCAPED_UNICODE);
$length = strlen($string);
You have to first put the json into a variable to count it, before outputting it.
To count the items in an array before you turn it into a json, you can do:
$length = count($array);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Ok i have a while loop function in my site that pulls in excel documents and parses them to the database i want to check for duplicates and if duplicate skip it:
$content = file($selectfile1);
$posted_content = array();
list($rownum, $row) = each($content);
$posted_content[0] = explode(",", $row);
array_push($posted_content[0], "ID");
$count = 0;
// iterate each row (1 post)
while (list($rownum, $row) = each($content))
{
$count++;
$cols = "orderid, created_at, updated_at, notification_type, radius, available, expiration, ";
$vals = "";
$cols2 = "equipment_id";
$vals2 = "";
....{parsing data)...
}
i want to write in a script that checks to see if the record is a duplicate and if not enter it.
$sql25 = "SELECT * FROM notifications WHERE origin =" . $origin_id . " user_id =12039";
$rs25 = $conn->Execute($sql25);
if($rs25->RecordCount() == 1 || $rs25->RecordCount() >= 1)
{
here is where i need a command. Can you use? next()
--------------------------------------------------
}
else
{
Insert query
}
You are looking for the continue statement.
From the docs:
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
(See http://www.php.net/manual/en/control-structures.continue.php)
example:
<?php
while ( ... ) {
if ($foo = 'bar') {
// skip to the next iteration
continue;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to find the best killer (most kills) out of a kills database.
Put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important.
How do I do that?
$array = array();
$index = 0;
while($mData = $q->fetch_assoc())
{
$index++;
$arr = explode('with ', $mData['killText']);
$unimportant = array(" (in paintball)", " (in event)");
$important = str_replace($unimportant, "", $arr[1]);
if(empty($important)) { $important = "Suicide"; }
$array[$important]['Kills']++;
$array[$important]['Gun'] = $important;
$query2 = $mysql->query("SELECT * FROM `kills` WHERE `killText` LIKE '%$important%' AND `killerID` = '". $mData['killerID'] ."'") or die($mysql->error);
while($kData = $query2->fetch_assoc())
{
// put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important
}
}
A GROUP BY will do the trick:
"SELECT killerID, COUNT(*) FROM kills WHERE killText LIKE '%$important%' GROUP BY killerID;"
You ca just fetch "killerID" and got the killer with most kills of weapon $important:
$kData = $query2->fetch_assoc();
$kData['killerID'];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I made a code that reads a table from another website and writes it on mine. Now I want to read just specific rows/columns and write it on my site. The table is filled with weather data and it refreshes every 5 minutes. I need only values for full and half hours and not all the values in the row, but just temperature. For example, there's a row for every five minutes containing temperature value, humidity, sun radiation etc. I need to find a value of, let's say 05:00, and read/write only temperature column of that row. In this case it would be: 05:00 12,5°C. And I need 48 values, because there's 24 hours per day and including another 24 half hours it's 48 all together, right..
This is a part of my code:
<?php
$trazi = ':00';
$citaj = file('proba.txt');
foreach($citaj as $linija)
{
if(strpos($linija, $trazi) !== false)
echo $linija;
}
$traziURL = "somepage";
$stranica = file_get_contents($traziURL);
$tablica = '/(<table.*<\/table>)/s';
preg_match_all($tablica, $stranica, $zeit);
echo $zeit[0][0];
$ime = "proba.txt";
$table = fopen($ime, 'w') or die ("Error!");
$podaci = $zeit[0][0];
fwrite($table, $podaci);
fclose($table);
?>
There's a chance that it won't work for you 'cause some parts are missing, but just to give you the idea.
I'm sure there are multiple other ways to do this, but I'd do it like this.
<?php
/**
* #author Bart Degryse
* #copyright 2013
*/
function getData() {
//Get the html page
$url = "http://www.essen-wetter.de/table.php";
$content = file_get_contents($url);
//Turn it into a dom document searchable by xpath
$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
//Get field names
$query = "//tr/td[position()=1 and normalize-space(text()) = 'Zeit']";
$entries = $xpath->query($query);
$entry = $entries->item(0);
$tr = $entry->parentNode;
foreach ($tr->getElementsByTagName("td") as $td) {
$fieldnames[] = $td->textContent;
}
//Get field data
$query = "//tr/td[position()=1 and (substring-after(normalize-space(text()),':') = '00' or substring-after(normalize-space(text()),':') = '30')]";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
$fieldvalues = array();
$tr = $entry->parentNode;
foreach ($tr->getElementsByTagName("td") as $td) {
$fieldvalues[] = $td->textContent;
}
$data[] = array_combine($fieldnames, $fieldvalues);
}
//Return data set
return $data;
}
//Gather the data
$data = getData();
//Do something with it
echo "<pre>\n";
foreach ($data as $row) {
echo "Temperature at {$row['Zeit']} was {$row['Temperatur']}.\n";
}
echo "</pre><hr><pre>\n";
print_r($data);
echo "</pre>\n";
?>
If you're going to display the data on a UTF-8 compatible terminal or on a web page that's declared as being UTF-8 encoded this should do it.
If you're want to use single-byte ISO-8859-1 encoding however you'll have to change this line:
$fieldnames[] = $td->textContent;
into this:
$fieldvalues[] = utf8_decode($td->textContent);
Remark
Please note that while doing this is technically not that hard legally you're on loose ground. The data on that page is copyrighted and owned by Markus Wolter. Using his data for your own purposes without his consent is considered theft.