1

In C the last character of a string is always '\0' Is it same for PHP as well? If not, then what's the last character of a string in PHP?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Sahasrangshu Guha
  • 672
  • 2
  • 11
  • 29
  • 3
    Nothing. PHP doesn't need a terminating character for strings. It handles these things internally. – Aziz Nov 15 '12 at 08:12
  • then if i want to run a loop from the first charecter to the last of the string how can i do that in php?? – Sahasrangshu Guha Nov 15 '12 at 08:13
  • What do you need the /0 for? If you just want to check length or manipulate the string in some way, there is a multitude of functions for that, see http://www.php.net/manual/en/ref.strings.php – Grüse Nov 15 '12 at 08:15
  • `for ($i=1; $i – Aziz Nov 15 '12 at 08:15
  • This could answer your question in more detail: http://stackoverflow.com/questions/4601032/php-iterate-on-string-characters – Grüse Nov 15 '12 at 08:15
  • i have a csv file containing some data for database.now i have to convert each line of data in sql format. the data in csv file is as follows:"abc,abc,1243,abc" I have to make them like thi s" 'abc','abc',123,'abc' ". – Sahasrangshu Guha Nov 15 '12 at 08:16
  • 1
    Use [`fgetcsv()`](http://php.net/manual/en/function.fgetcsv.php) for parsing csv files. – Decent Dabbler Nov 15 '12 at 08:23

4 Answers4

3

Strings in PHP are binary safe, which means that they can contain \0 as well. Therefore, there's no termination character that could be used.

Instead, the length of the string is stored separately inside PHP's internal variable representation. This also makes string length calculations much faster.

i have a csv file containing some data for database.now i have to convert each line of data in sql format. the data in csv file is as follows:"abc,abc,1243,abc" I have to make them like thi s" 'abc','abc',123,'abc' ".

First of all, you should use fgetcsv() for that; it turns the whole line (or lines in some cases) into one array.

I'm going to guess you want to store those values into a database; you could use this:

if ($arr = fgetcsv($f)) {
    $stmt = $db->prepare('INSERT INTO tablename VALUES (?, ?, ?, ?)');
    $stmt->execute($arr);
}

This assumes each line in your CSV has four items; to make it more generic you could use the length of $arr to construct the number of place holders to add in the query.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 2
    @AngshuGuha If it solved your problem, then why accept the other answer? ;-) – Ja͢ck Nov 15 '12 at 12:33
  • 2
    @AngshuGuha Normally that's what you would use upvote for; the answer that solves the problem is typically the one that gets accepted. – Ja͢ck Nov 15 '12 at 14:02
1

To loop over the characters in a string, you write:

for (i = 0; i < strlen($string); i++) {
    $char = substr($string, $i, 1);
    // Do stuff with $char
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

I think its should be worked

  $lastChar = substr($string, -1);
  • The question is more about a string terminating character, not so much about finding the actually last character. Even if it was, I'd not reverse (copy) the string, when a simple string length function would do. – Robert Mar 31 '17 at 14:21
0
for( $i = 0, $len = strlen( $string ); $i < $len; $i++ )
{
    $char = $string[ $i ];
    // do something with $char
}

... is one way to do it.

Another way, is simply using PHP function str_split() (which creates an array of the individual characters of the string (unless you pass it the second argument $split_length of a different length than the default)):

foreach( str_split( $string ) as $char )
{
    // do something with $char
}

... etc.

Edit:

As an addendum to your comments to your question; use fgetcsv() for parsing csv files.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106