Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have a script, Can be found here:
http://ddelay.co.uk/bus/find_services2.php
With the following code:
<?php
include('simple_html_dom.php');
$service = $_GET["stop"];
$debug = $_GET["debug"];
$url = $service;
if($debug === "yes"){
echo "URL IS: " . $url . "\n";
}
$search = array('?', ' ', '.asp&');
$replace = array('&', '+', '.asp?');
$url2 = str_replace($search, $replace, $url);
if($debug === "yes"){
echo "REPLACEMENTS: ". $url2 . "\n";
}
$end = "http://tsy.acislive.com" . $url2 . '&showall=1';
if($debug === "yes"){
echo "FINAL URL: ". $end;
}
$html = file_get_html($end);
$ret = $html-> getElementsByTagName('table');
print($ret);
?>
For example which will pull the table from tsy.acislive.com (example: http://ddelay.co.uk/bus/find_services2.php?stop=/web/public_service_stops.asp?service=22?operatorid=36?systemid=30?goingto=Woodhouse)
I then want to be able to convert this table to JSON data to use in my app. Unfortunately I have tried PHP's function JSON_encode($ret); but unfortunately that failed. Would anybody know of how I can convert this table pulled using Simple-Dom-Parser php into Json Data
If you want to convert to JSON, json_encode is the way you should do it. It's a really easy to use function. If you're having trouble with it, there'll be an underlying reason.
Try these to find out what your data looks like:
var_dump($html);
var_dump($ret);
From the PHP manual, the value passed to json_encode Can be any type except a resource., so if it's failing I can only assume that you aren't passing it the correct data.
Post the results of those var_dump calls and I'll edit this with a solution for you.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$defaultdata = "abcdef00000000000000000000000000";
$data1 = "271";
$output = "abcdef00000000000000000000000271";
How can I replace the string based on the data. For example, if the default data is abcdef00000000000000000000000000 , so it will replace when the data1 got value. So the output will be abcdef00000000000000000000000271. How can I do this?
I'd use substr:
$output = substr($defaultdata,0,strlen($defaultdata)-strlen($data1)) . $data1;
Applying it to your code
<?php
$defaultdata = "abcdef00000000000000000000000000";
$data1 = "271";
$output = substr($defaultdata,0,strlen($defaultdata)-strlen($data1)) . $data1;
echo $output;
?>
$output = substr($defaultdata, 0, strlen($defaultdata) - strlen($data1));
$output .= $data1
I think the easiest way to solve this is to use str_pad. It is a built in function made to tackle situations like yours. Then you can easly swap defaultdata to something other. The code is as follow:
$output = str_pad($data1, strlen($defaultdata), $defaultdata, STR_PAD_LEFT);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using simple HTML textarea I added some records in MySQL. While adding the entries I pressed enter to separate the lines. Now I want to select/display those records using PHP in bullets. I want each lines to be displayed into bullets.
E.g (the things I have added)
Name
Age
Gender
What I want
Name
Age
Gender
I am using following PHP script
$result = mysql_query("SELECT * FROM students");
echo "<ul>";
while($row = mysql_fetch_array($result))
{
echo '<li'.$row["details"].'</li>';
}
echo "</ul>";
While i'm not sure it's a good practice (what if the users forgot to use a newline/enter?) you can use the explode() function.
From the manual:
Returns an array of strings, each of which is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
$listItems = explode("\n\n", $row['details']);
echo '<ul'>;
foreach($listItems as $item){
echo '<li>'. $item .'</li>';
}
echo '</ul>';
Please note: You're using mysql_ which is deprecated. Consider using PDO.
This should do the trick:
//assuming $result variable holds the db value from the textarea
$lines = explode("\n", $result);
echo "<ul>;
array_walk($lines, function($line) {
$sanitizedLine = htmlentities($line);
echo "<li>$sanitizedLine</li>";
});
echo "</ul>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to scrape the content post of this forum https://forum.lowyat.net/topic/3424996 using below code.
$rows = $html->find('.post_table');
$array = array();
foreach($rows as $go){
$post_text = $go->find('.post_td_right > .post_text')->innertext;
$array[]= array(
'content'=> $post_text
);
}
echo json_encode($array);
I var_dump($rows) and it's an object, I really don't know why is the mistake. Need your help!
Forums usually have an RSS feed to help with this sort of requirement. Turns out, the site you're scraping supplies this for you: http://rss.forum.lowyat.net/topic/3424996
We can now use an XML parser instead of a DOM scraper, which will be much more efficient. For example;
<?php
$rss = file_get_contents('http://rss.forum.lowyat.net/topic/3424996'); //Or use cURL
$xml = simplexml_load_string($rss);
$array = array();
foreach($xml->channel->item as $posts) {
$post = (array) $posts->description;
$array[] = htmlentities($post[0]);
}
echo "<pre>";
echo print_r($array);
echo "</pre>";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on a message filtering algorithm to allow links from the parent website and replace even the minutest trace of links from an external website in any form.
Would appreciate a wholesome solution on the same from the perspective of PHP.
From what I know, this can be accomplished 4 in basic steps -
1. Detect URLS in the string using preg_match_all
2. Sanitize them to valid URL formats
3. Use parse_url to check the host value and replace the old string appropriately.
4.Convert urls to html friendly clickable links.
Also, I am using the Yii framework if it helps.
Edit : Pasting my code here, suggestions are welcome.
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';
$message = preg_replace_callback("#$pattern#i", function($matches) {
$hostname = Yii::app()->params['baseUrlName'];
$input = $matches[0];
$url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
$data = parse_url($url);
if(isset($data['host']) && $data['host']==$hostname)
{ return '' . "$input"; }
else
{ return ' x '; } }, $message);
Here is an image of the $pattern:
(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))
It can be reduced like this:
(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:(?P<BPC>[^\s()<>]+)|\(((?&BPC)|(\((?&BPC)\)))*\))+(?:\(((?&BPC)|(\((?&BPC)\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))
This is currently the best solution to go about it.
$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:(?P<BPC>[^\s()<>]+)|\(((?&BPC)|(\((?&BPC)\)))*\))+(?:\(((?&BPC)|(\((?&BPC)\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';
$message = preg_replace_callback("#$pattern#i", function($matches) {
$hostname = Yii::app()->params['baseUrlName'];
$input = $matches[0];
$url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
$data = parse_url($url);
if(isset($data['host']) && $data['host']==$hostname)
{ return '' . "$input"; }
else
{ return ' x '; } }, $message);
Update - edited with the regex tweak from Alex.
I coded a script longago which is extremely popular however i am about to role out a update using the clever in-app-auto update feature i coded. Now the question is i have lost the JSON data i was displaying to the code which it then decodes and reads and gets the relevant content.
Here is a snippet:
function check_update(){
$v = isset($_GET['v']) ? $_GET['v']:11;
$reqUrl = "http://myserver.myhost.com/api/update/?" . "v=" . $v;
return json_decode(#file_get_contents($reqUrl));
}
function updatefunction(){
$updateResult = check_update();
print $updateResult->content . "\n\r";
}
echo updatefunction();
I have added the "echo updatefunction();" as a debug test for me to see if it is getting the correct data.
Now my question is what should the $reqUrl be showing for this script to echo some content ?
Thanks