here is my code, I want to print single space between each array element
problem i am facing is m getting space on every element but there is space for 1st element also
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');
for($i=sizeof($arr);$i>=0;$i--)
{
echo $arr[$i]." ";
}
?>
my output is " 2 3 4 1" i want "2 3 4 1"
there is space in the 1st element.
use ltrim() it will remove space from left end
read documentation for further details
ltrim will remove spaces from left side
echo ltrim($arr[$i])." ";
Related
I have this kind of text file.
"u901_humext ""2019-02-01 23:24"" 99.74 99.9 99.82".
And I want to read line one by one and put into a variable like:
name = u901_humext
date = 2019-02-01 23:24
min = 99.74
But I don't know how to split it.
You can do this as below:
$string = '"u901_humext ""2019-02-01 23:24"" 99.74 99.9 99.82"';
$explodedArray = explode('"', $string);
foreach($explodedArray as $key => $element){
$explodedArray[$key] = trim($element, ' "');
}
echo "Name: ".$explodedArray[0]."\n";
echo "Date: ".$explodedArray[1]."\n";
echo "Min: ".$explodedArray[2];
Explanation:
Firstly split the string using explode() function of PHP.
Then loop through each element of the array. Trim each element of the array to remove "(double quote) and (space).
Print the elements
hello can anyone help me to solve this probelm how to solve with this prblem, I wanna make sentence from this paragraph, i use the point as a separator paragraphs, but I have problem if a paragraph has a nominal number has a point that should be one part of the sentence. can anyone help me???
//$paragraf1 = $paragraf;
if(preg_match("/.*[0-9][.]/", $isi) != ""){
$isi = preg_replace("/([.])/", "", $isi);
}
else
{
return $isi;
}
this code to separator paragraf with point ==> $paragraf = explode(".", $isi);
if(strlen($paragraf[count($paragraf)-1]) <= 1) unset($paragraf[count($paragraf)-1]);
//1. Case Folding
for($i=0; $i<count($paragraf); $i++){
//Merubah ke huruf kecil
$paragraf[$i] = strtolower($paragraf[$i]);
//menghilangkan tanda baca
$paragraf[$i] = preg_replace("/[^A-Za-z0-9 ]/", "", $paragraf[$i]);
}
//$_SESSION['paragraf'] = $paragraf;
print_r($paragraf); echo "<br/>";
exit();
enter image description here
Simple solution should be that you explode on this
explode(". ", $isi);
Because well formated text has empty space after point and also numbers don't have this empty space.
Also if text is not well formated that means you need after exploding text to make a loop and to check every element in array if it has number on its end(last char) and on its begining (first char) of next element and if it has to join this two elements.
I am entering text in the database in two paragraphs
first paragraph
second paragraph
I am storing them in my database and when I am displaying them on the frontend using nl2br it is getting displayed perfectly.
I want the my first paragraph to be bold and the second paragraph should be normal.
I tried using strpos to find the location of the <br> tag after nl2br to chop off the first paragraph but I am not succeeding.
the failed code is
echo strpos(nl2br($row['article']), "<br>");
but i am not getting the position of the <br> tag
I got the correct answer from eddie, he deleted it but i am updating the answer here
$str='first paragraph
second paragraph';
foreach(explode("\n",) as $key => $val) {
if($key == 0){
echo'<b>';
}
echo $val;
echo'<br>';
if($key == 0){
echo '</b>';
}
}
Don’t use nl2br for the type of results you are looking for. Just split the string into an array using a regex rule with preg_split and then act on the first item in the array. Here is test code:
// Set the test data.
$test_data = <<<EOT
first paragraph
second paragraph
EOT;
// Split the test data into an array of lines.
$line_array = preg_split('/(\r?\n){1,2}/', $test_data);
// Roll through the line array & act on the first line.
$final_text = '';
foreach ($line_array as $line_key => $line_value) {
if ($line_key == 0) {
$line_value = "<b>" . $line_value . "</b>";
}
$final_text .= $line_value . "<br />\n";
}
// Dump the line array for debugging.
echo '<pre>';
print_r($line_array);
echo '</pre>';
// Echo the final text.
echo '<pre>';
echo htmlentities($final_text);
echo '</pre>';
die();
The output from the dump of the line array would be this:
Array
(
[0] => first paragraph
[1] => second paragraph
)
And the test output using htmlentities to show what was done HTML-wise:
<b>first paragraph</b><br />
second paragraph<br />
Try:
$first_line = explode(PHP_EOL, $str)[0];
$new_str = str_replace($first_line,'<b>'.$first_line.'</b>',nl2br($str));
How can i only echo 5 lines?
<?php
$text = Here i have a long long long text about 20 lines;
echo $text;
?>
So and i want something like that.->
TEXTLINE 1
TEXTLINE 2
TEXTLINE 3
TEXTLINE 4
TEXTLINE 5
And than stop.
Code for the mentioned text of Harri (at least this would be my approach):
$strings = explode(" ", $text);
for($i=0; $i<$your_lines; $i++) {
echo $strings[$i] . " ";
}
Explode the string to array, then loop through the array until last line you want to print. Printh each line while looping the array.
when you need lines (not words) use
$lines = explode(PHP_EOL, $text);
Assuming the output is a web browser, then this is a display issue, since what you are referring to as "line" depends on the width/height of the container. (It's not very clear what you try to ask)
You can set a width and height on a div using css and use overflow hidden on that to achieve a result like the one you want
demo
http://jsfiddle.net/g5Y5c/
.mydiv {width:100px;height:100px;overflow:hidden;}
I need to add itens from a text file, into a MySQL database, to do that i'm trying to use PHP to read the file and insert everything into the database, the problem is that the text file, has 2 itens per line, divided by spaces, something like this:
teste.txt
ITEM1 ITEM2
ITEM323 ITEM4
ITEM54 ITEM6
ITEM34234 ITEM8
I'm trying to remove the spaces using explode, but because the number of spaces, is random, i cannot do that. This is my Code:
$handle = #fopen("teste.txt", "r"); //read line one by one
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
//values.=($a,$b);// save values and use insert query at last or
echo $a;
echo "<br>";
echo $b . "<br>"; // NEVER echoes anything
}
What should i do?
You can insert a method to change multiple spaces to one:
$buffer = preg_replace('/\s+/',' ',$buffer);
Right before exploding it:
$buffer = fgets($handle, 4096); // Read a line.
$buffer = preg_replace('/\s+/',' ',$buffer);
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
You can use preg_replace() to perform a regular expression:
$buffer = preg_replace("/\ +/", " ", fgets($handle, 4096));