Jump to content

general question. regarding php functions


piyush23424

Recommended Posts

Hi,

do we have any php inbuilt function which returns true or false after checking a particular character (need to check comma) in a given string.

actually i want to conditionally explode a string i.e if string is having comma it will explode it by comma otherwise by space.

 

Thanks

Hi,

do we have any php inbuilt function which returns true or false after checking a particular character (need to check comma) in a given string.

actually i want to conditionally explode a string i.e if string is having comma it will explode it by comma otherwise by space.

 

Thanks

 

how about:

 

if (preg_match(","$string))
{
    $string_array = explode(",",$string);
}

 

That should do the trick.

if (preg_match(",",$searchString))

    {

      $string_array = explode(",", $searchString);

    }

  else

    {

      $string_array = explode(" ", $searchString);

}

  echo "<pre>";

  print_r($string_array);

 

Output is warning error :

Severity: Warning

Message: preg_match() [function.preg-match]: No ending delimiter ',' found

Filename: controllers/cma.php

Line Number: 104

 

but its doing explode with space

Array

(

    [0] => 779,TAMIAMI

    [1] => TRL

    [2] => #6

)

if (preg_match(",",$searchString))

    {

      $string_array = explode(",", $searchString);

    }

  else

    {

      $string_array = explode(" ", $searchString);

}

  echo "<pre>";

  print_r($string_array);

 

Output is warning error :

Severity: Warning

Message: preg_match() [function.preg-match]: No ending delimiter ',' found

Filename: controllers/cma.php

Line Number: 104

 

but its doing explode with space

Array

(

    [0] => 779,TAMIAMI

    [1] => TRL

    [2] => #6

)

 

I never tested the code, the following code works for me:

 

$string = "one,2,three,4";
if (preg_match("/,/",$string))
{
    $string_array = explode(",",$string);
}
echo "<pre>";
print_r($string_array);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.