Count lines in array php

php help

Solution:
it is very easy in php , you can explode that string with ‘\n’ that is used for new line

Example Code:

$string=”1\n2\n3\n”; // there are 3 lines in a string
$array= explode(“\n”, $string);
print “Total No. of lines are “.count($array)-1;
?>

Share this:

Like this:

9 Comments »

Tam said

August 8, 2009 @ 2:59 pm Do you test with this? When I test, it output -1.
Any problem? Plz mail to vo.mita.ov@gmail.com because I donot go here regularly. Thank you!

datshay said

amitmondal said

February 14, 2011 @ 8:19 am Nice tutorial….thanks for this post…..
Please visit this link for learn more script.
http://amitmondal.wordpress.com/

RF said

February 28, 2011 @ 10:57 pm What if the string was a text blob from an SQL database. Is it possible to a number to each line for display in a textarea?

Glen Boonzaier said

June 29, 2011 @ 2:16 pm $string=”1\n2\n3\n”; // there are 3 lines in a string
$array= explode(“\n”, trim($string));
foreach($i=1;$i <=count($array);$i++)<
$array[$i] = «$i: » . $array[$i];
>
echo implode(‘\n’,$array[$i]);

Glen Boonzaier said

Glen Boonzaier said

June 29, 2011 @ 1:46 pm Here is a better version: $string=”1\n2\n3\n”; // there are 3 lines in a string
$array= explode(“\n”, trim($string));
print “Total No. of lines are “.count($array); This will eliminate any whitespace trailers including new lines.

Rishi said

September 15, 2012 @ 8:45 am $string=”
1
3
“;
$array= explode(‘
‘, $string);
echo “Total No. of lines are “.count($array);

Источник

count lines in array and split into 2 divs php

To fix your first issue, use sizeof(array) rather than a specific number, e.g.:

for($i = 0; $i < sizeof($array); $i++)< $dateAndText= explode(' ', $array[$i], 2); echo 'Edit: Your third column would look a bit like this:

There are a few issues going on here.

  1. If you delete the condition, either your for loop is erroring out or it's going on forever (depends on how you're trying to delete it). To get all the records, you want for ($i = 0; $i < count($array); $i += 1) < .
  2. To limit the length of the first column of 'projets' to the length of 'conferences', you'll need to pick up the data ahead of time, then display it. Here's what I mean:
$projets = explode("\n", get_field('projets')); $conferences = explode("\n", get_field('conferences')); $monographies = explode("\n", get_field('monographies')); // Now we want to limit the length of the first column of projets (col 1) to the // length of conferences (col 3). $projets = array_chunk($projets, count($conferences)); // Split the projets into a set of arrays of the right length. $projets1 = array_shift($projets); // Grab the first one. that's the data for the first column. $projets2 = array_reduce($projets, 'array_merge', array()); // Merge the remaining arrays into a single array for display. 
function displayDateAndText ($obj) < $dateAndText = explode(' ', $obj, 2); echo "function displayRecords ($records) < echo '

'; $limit = count($records); for ($i = 0; $i < $limit; $i += 1) < displayDateAndText($records[$i]); >echo '

'; > ?>

More Answer

  • PHP - How to split array into variables and pass to a function?
  • php - split string by spaces and quotes without spaces into array
  • Split array into 3 pieces and put it in separated divs
  • Copying specific keys and values of a PHP multidimensional array into another array
  • PHP split up to every nth line into an array
  • PHP Split string output up randomly insert into array
  • How to split an array into pairs of different elements in PHP randomly?
  • How to read multiple line from file and split it to array in php
  • how to count values in php array and while loop
  • PHP split csv line into multidimensional array
  • PHP Exploding first part of a string into array elements and the second part into one element
  • Better way to count and construct unique array in PHP
  • How to pass php array to jquery and turn into json?
  • php split string into array by date (yyyy-mm-dd)
  • Parse file contents into array of section headings and section lines
  • Text file lines into array with PHP
  • Split a string text by element class using php and keeping all original text inside array
  • count each character for each word in a string and put the word with less than 5 characters in array using php
  • How to generically turn self closing divs into using PHP and preg_match_all
  • PHP split mysql data into multiple lines
  • how to split a 2D array into array chunks on the basis of a index value in PHP
  • Splitting array of dates into year and months PHP
  • PHP foreach sorting every 9 items into lists and divs
  • PHP Search array recursively and return count of index?
  • PHP reading first 2 lines of file into variable and cylce through subfolders
  • how to split off first "column" of a php associative array of strings into a simple array of strings
  • How To Split A Line From An Array Into Two Variables Using PHP
  • How do I sort an associative array and split into two div's based on a value?
  • php string to array and split
  • How to store the contents of an array into a CSV file and save it in a location in php
  • PHP Associative Array - Split comma separated value into separate array rows
  • Count and sum values from array in PHP
  • explode and trim numbers into array from a string in PHP
  • How to Split Array Value same first character into a key and last character into a value
  • How to get each element in array and split them into two variables from a delimiter
  • pass php array into javascript, check array value and push into new array
  • How to concatenate a string, variable and an array modifier into a variable in php
  • How to get different count of key php array to table and
  • How to filter associative array by values and split into two variables?
  • Merging array values into single key and multiple values in php
  • PHP Regex getting data from String and into array
  • Breaking a PHP array into parts and running chunks asynchronously?
  • spiltting an array into n arrays and accessing individual arrays in php
  • Split HTML tr elements into PHP array
  • Spliting an array in php into keys and values
  • How to split a comma separated string into groups of 2 each and then convert all these groups to an array
  • PHP split UTF8 text into words and punctuation
  • simple way in PHP to split path string into breadth-first array
  • how to take the values from an input and a php array into a js and multiply?
  • Split HTML document into words and spans using PHP

More answer with same ag

  • convert doc to html by php script
  • javascript array returns [object Window] instead of array content
  • One Codeigniter website and application together
  • Else clause not functioning as expected
  • PHP foreach array and compare with GET
  • jquery ui creates a lot of undesirable spans
  • Get stream from imagecopyresampled
  • Magento : How to track or best way of tracking a customer session?
  • php file upload different types of files
  • Yii cleaning URLs
  • Problems with route in Laravel
  • How to adjust width of cells so they do not cut words in Spreadsheet_Excel_Writer
  • Can phpunit nativly test a jsonp API
  • Unable to catch a global exception
  • How to upload file into my server php
  • How to enable XMLReader in PHP without reconfiguring PHP?
  • PHP syntax error issue
  • Non-static method STemplate::assign() should not be called statically
  • PHP: get_called_class() returns unexpected value
  • is there Any Way to edit Many To Many Relation Ship without attaching-detaching in Laravel
  • indexing documents in Apache Solr using php-curl library
  • QBXML : Add Invoice to quick book online essential account with QBOE App /QBXML
  • PHP class for Aztec 2D Codes?
  • Blade syntax @extends loads second HTML view and then the first HTML view
  • Array in foreachloop returns additional null value
  • PHP - How do I modify variables within an array?
  • PHP setTime() return type not compatible but setTime() in DateTime class has no return?
  • HSQL, In-memory databases, Java and PHP question
  • php - Class not found (composer.json)
  • ajax supposed to add a session variable but not working
  • htaccess Permanent redirect from extension to slug
  • SimpleTest PHP not completing a testor showing a failure
  • how to pass by reference a class var to a global var in PHP?
  • IMGUR API - Permission Denied
  • swiftmailer and Yii2 not working 1 out of 10 time
  • HTML in JSON that "should" not be there
  • Block direct access to PHP files and allow json
  • .htaccess Multiple rewrites?
  • Paypal error We’re sorry Things don’t appear to be working at the moment
  • Selecting columns from array in Doctrine QueryBuilder
  • what is session_reset() exactly?
  • Echo HTML code, which is retrieved from a external page in php
  • How can i re-write the query in cakephp-3 way?
  • HTML5 Websocket sending simultaneous data to server
  • Is it possible to fetch ASP.NET or PHP code from a webservers .asp/.aspx/.php files?
  • php class magick function __call doesn't work
  • jQuery AJAX live update on multiple elements on the same page
  • Symfony2 ModelTransformer reverseTransform is never called
  • Where is the tutorial "processor" generated?
  • Silex slugs cause 404 page not found

Источник

Читайте также:  Label form control html
Оцените статью