How to split MySQL entry into different lines using PHP [closed] - php

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>

Related

Replace character depend on the data [closed]

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);

Php string If contain echo that string [closed]

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
My Php mysql out put is array i need if string contain particular character i need print that string only
while($row2 = mysqli_fetch_array($result2)) {
echo $row2['link'];
}
Output is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
i need like this
if (strpos($a,'videoweed') !== false) {
echo $row2['link'];
}
it show out put is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
I need output only;
http://www.videoweed.es/file/b38300afda3e6
You have answer in your code. Just need to assemble it. check this:-
while($row2 = mysqli_fetch_array($result2)) {
if (strpos($row2['link'],'videoweed') !== false) {
echo $row2['link'];
}
}

Need help in DOM traverse using php [closed]

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>";

How to separate text with PHP [closed]

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
Well I am trying to make a simple php system.
Anywise I need to separate the text when I want to add it to the database.
So for example I want to add:
abc:123
I want that the : will be the separater, so it'll look like this:
abc
123
And then both will go to a different table.
Could someone help me with this? As I am not an experience PHP coder, yet I am willing to learn how to do this.
Kind regards
This is pretty basic stuff..
$data = explode(':','abc:123');
foreach($data as $word)
{
// some code here
}
Use Split:
<?php
$data = "abc:123";
list ($var1, $var2) = split (':', $data);
echo "Var1: $var1; Var2: $var2;<br />\n";
?>
You can achieve this using explode.
abc:123
Is a string. Let's define it as a variable:
$origin = "abc:123";
You can split the string, using : as the separator.
$separator = ":";
$exploded = explode($separator, $origin);
Now you have an array which you can use to access abc and 123 individually.
$pre = $exploded[0];
$post = $exploded[1];
You don't know how many splits there will be?
That's okay. Your array simply increases, meaning you can simply loop through the array and handle the values.
foreach ($exploded as $split)
{
// Do something with $split
}

Scraping & Returning JSON data php [closed]

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.

Categories