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
Related
I am working on scraping and then parsing an HTML string to get the two URL parameters inside the href. After scraping the element I need, $description, the full string ready for parsing is:
<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>
Below I use the explode parameter to split the $description variable string based on the = delimiter. I then further explode based on the double quote delimiter.
Problem I need to solve: I want to only print the numbers for MeetingID parameter before the double quote, "773".
<?php
echo "Description is: " . htmlentities($description); // prints the string referenced above
$htarray = explode('=', $description); // explode the $description string which includes the link. ... then, find out where the MeetingID is located
echo $htarray[4] . "<br>"; // this will print the string which includes the meeting ID: "773">Description</a><br>"
$meetingID = $htarray[4];
echo "Meeting ID is " . substr($meetingID,0,3);
?>
The above echo statement using substr works to print the meeting ID, 773.
However, I want to make this bulletproof in the event MeetingID parameter exceeds 999, then we would need 4 characters. So that's why I want to delimit it by the double quotes, so it prints all numbers before the double quotes.
I try below to isolate all of the amount before the double quotes... but it isn't seeming to work correctly yet.
<?php
$htarray = explode('"', $meetingID); // split the $meetingID string based on the " delimiter
echo "Meeting ID0 is " . $meetingID[0] ; // this prints just the first number, 7
echo "Meeting ID1 is " . $meetingID[1] ; // this prints just the second number, 7
echo "Meeting ID2 is " . $meetingID[2] ; // this prints just the third number, 3
?>
Question, why is the array $meetingID[0] not printing the THREE numbers before the delimiter, ", but rather just printing a single number? If the explode function works properly, shouldn't it be splitting the string referenced above based on the double quotes, into just two elements? The string is
"773">Description</a><br>"
So I can't understand why when echoing after the explode with double quote delimiter, it's only printing one number at a time..
The reason you're getting the wrong response is because you're using the wrong variable.
$htarray = explode('"', $meetingID);
echo "Meeting ID0 is " . $meetingID[0] ; // this prints just the first number, 7
echo "Meeting ID1 is " . $meetingID[1] ; // this prints just the second number, 7
echo "Meeting ID2 is " . $meetingID[2] ; // this prints just the third number, 3
echo "Meeting ID is " . $htarray[0] ; // this prints 773
There's an easier way to do this though, using regular expressions:
$description = '<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>';
$meetingID = "Not found";
if (preg_match('/MeetingID=([0-9]+)/', $description, $matches)) {
$meetingID = $matches[1];
}
echo "Meeting ID is " . $meetingID;
// this prints 773 or Not found if $description does not contain a (numeric) MeetingID value
There is a very easy way to do it:
Your Str:
$str ='<a target="_blank" href="CoverSheet.aspx?ItemID=18833&MeetingID=773">Description</a><br>';
Make substr:
$params = substr( $str, strpos( $str, 'ItemID'), strpos( $str, '">') - strpos( $str, 'ItemID') );
You will get substr like this :
ItemID=18833&MeetingID=773
Now do whatever you want to do!
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])." ";
I have a text file of phone numbers like below:
+2348089219281 +2348081231580 +2347088911847 +2347082645764 +2348121718153 +2348126315930 +2348023646683.
I want to extract each number, strip +234 from it and replace with 0 , then add the following text "Names" . "\t" in front of the modified number.
Then I want to insert this new string in a new text file (line-by-line)..
This is what I get in the new_textFile with the code I've wrote:
Names 00urce id #3
Names 00urce id #3
Here's my code:
$this_crap_file = fopen($old_file_name, "r");
$total_number_lines_for_this_crap_file = count($this_crap_file);
while(!feof($this_crap_file))
{
$the_new_writing = fopen($new_file_name, "a");
$the_string = substr_replace($this_crap_file, "0", 0, 4);
$new_string = "Names" . "\t" . 0 . $the_string . "\n";
fwrite($the_new_writing, $new_string);
}
fclose($this_crap_file);
No space between fopen and the parenthesis? Sorry, I don't see the relevance of that statement.
Assuming your input file only has one phone number per line, and that they all start with '+234', you could use a regular expression to pick out just the part you want to put in the new file, like this:
$this_crap_file = fopen($old_file_name, "r");
$the_new_writing = fopen($new_file_name, "a");
while ($line = fgets($this_crap_file))
{
preg_match('/\+234(\d+)/', $line, $matches);
$new_string = "Names\t" . $matches[1] . "\n";
fwrite($the_new_writing, $new_string);
}
fclose($the_new_writing);
fclose($this_crap_file);
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));
I'm using this string to get what's needed:
echo "<div id=\"lot\">".$table->children(2)->children(0)->plaintext."</div>";
And I get the result: 01 02 03 04 05 06 10 15 17 27 31 etc.
What I need is to add <div id=\"new\"></div> to each number.
<div id=\"new\">01</div> <div id=\"new\">02</div> etc etc.
I tried to use the code below but it doesn't work
$table = $box_rezult->find("table.last-d", 0);
$results = $table->children(2)->children(0)->plaintext;
$string = $results;
$var1 = str_replace(" ", "<div id=\"new\"> </div>", $string);
echo $var1;
I would appreciate any help, thanks!
Thanks to everyone!!
explode() the string to an array, then implode() it:
echo '<div class="new">' . implode('</div><div class="new">', explode(' ', $string)) . '</div>';
I've changed id to class as id's must be unique.
Demo
For your use case you need to flip tags to get a separator and pre- and append one more:
$var1 = "<div id=\"new\">" . str_replace(" ", "</div> <div id=\"new\">", $string) . "</div>";
Assuming that $string contains all the numbers you want,
you can explode it and put each element you want to have in tags for each number and then apply it to string, using a loop (or implode, as suggested):
$numbers = explode(" ", $string);
$var1 = "";
foreach($numbers as $number){
$var1 .= "<div id=\"new\">".$number."</div>";
}
Also, please take into consideration about id attribute uniqueness. Use class attribute instead.