Jump to content
xisto Community
Sign in to follow this  
veerumits

Two Method Are Preg_split() And Explode() Are Used To Split The Pattern Then what is the difference

Recommended Posts

Two method are preg_split() and explode() are used to split the pattern:
when and where we can use these method let us i explain

<?php$s ='on|tp|tr|fr';print_r(explode('|', $s, 2));?>Array([0] => on[1] => tp|tr|fr)

if we want to split
"Hello"

Then explode() is not work because it must be require one identifier.
and there is no identifier in "Hello" so we can not use explode(),
but by preg_split() we can split the pettern

how we use

<?php$x="Hello";$y=preg_split("", $x);// please help there what we write so it workprint_r($y); // [0]=>H [1]=>e [2]=>l [3]=>l [4]=>0?><?php$x="Anil", Raju and Mukesh";$y=preg_split("/,| and /", $x);print_r($y); // [0]=>Anil [1]=>Raju [2]=>Mukesh?>

it works fine

thanks

Share this post


Link to post
Share on other sites

preg_split allows you to use regular expressions for more complicated pattern matching (regular expressions). If you don't need this, then you should be using explode because I'm fairly confident it's faster. If you want to split a string into individual characters, then you can use str_split, for example str_split("Hello") is the same as preg_split("","Hello").

Share this post


Link to post
Share on other sites

You don't even have to split it though. If you call $x="Hello" then $x[0] will be "H" and $x[1] will be "e" and so on. Just a hint :)

I've heard you can do that in some languages, I wasn't sure about PHP though so I didn't say :) Thanks for the information though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.