Jump to content

implode and exsplode need more examples please


redarrow

Recommended Posts

I am trying to get to grips with implode and exsplode but need more proper examples and exsplenations thank you..

Is all this string miniplnation?

example implode

I understand this the implode gets the string and then adds the and.
[code]
<?
$num=array("one","two","three","four");
$result=implode(" and " , $num);
echo $result;
?>
[/code]


exsplode example
quit dont get to grips with this the explode gets the string the with
any space out puts the result with a for loop.

do you need always to use for loops on explode?
[code]
<?
$sentence="my little cat";
$result=explode(" " , $sentence);
for($i=0; $i< count($result); $i++) {
echo " number is ".$i." and ".$result[$i]."<br>";
}
?>
[/code]


thank you for your support.
Link to comment
Share on other sites

Implode syntax:
string [b]implode[/b] ( string glue, array pieces )

the glue bit is the sting or range of characters that gets inserted between values within an array. which is the pieces part. So for example. If you have any array that stores names or words and yiu want an easy way of displaying this arrat you'll want to use implode. Now you want to display your name or words seperated by a coma. This is how you'll do it:
[code]<?php

$num = array("one", "two", "three"," four");
$result = implode(", " , $num);
echo $result;

?>[/code]That will will result in this: [i]one, two, three, four[/i]



Explode syntax:
array [b]explode[/b] ( string separator, string string [, int limit] )

Explode is alittle diferent to implode as Explode custs up strings, depending on the seperator specified and puts each cut section into an array. So take this for example:
So you have string like so:
[i]hello I am redarrow[/i] and you want each word to be put into an array you'll soiemthing like this:
[code]<?php

$string = "Hello I am redarrow";
$words = explode(" ", $string);

?>[/code]You'll now get an any like so:
[i]array("Hello", "I", "am", "redarrow")[/i]

You dont have to use a for loop no. As you can echo out each word sperately like so:
[code]echo $words[0];
echo $words[1];
//ect.[/code]

Thats all there is to it. To get a better explanation always check the PHP manul for thse two functions
[a href=\"http://www.php.net/implode\" target=\"_blank\"]implode[/a]
[a href=\"http://www.php.net/explode\" target=\"_blank\"]explode[/a]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.