Jump to content

Split a string - or can you?


ddrake1984

Recommended Posts

$string = 'ThisText';

$newstring = '';
foreach( $string AS $letter ) {
    $newstring .= ( ctype_upper( $letter ) ? ' ' : '' ) . $letter;
}
$string = trim( $newstring );

Since this will put a space in front of every capital letter, I put the trim on the end to remove the space from the beginning.

Link to comment
Share on other sites

rythemton your script unfortunately didnt work. I have this error

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\index.php on line 52

 

dragon_sa yours worked but only one string worked. there was an error after the first string.

 

Fatal error: Cannot redeclare splitByCaps() (previously declared in C:\xampp\htdocs\index.php:51) in C:\xampp\htdocs\index.php on line 51

 

my code is:

 

while(odbc_fetch_row($sqlresult_products))

{

  echo "<tr>";

  echo "<td><font size=1 face=verdana><b>";

  $string = substr($phpresult_vendors,9,-9);

 

  function splitByCaps($string){

  return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string);

}

echo splitByCaps($string);

 

for the purpose of understanding the above script a bit better $phpresult_vendors are 'Products_IngramMicro_Products' 'Products_ExpressData_Products' 'Products_TheAmazingCompany_Products" etc.

 

Link to comment
Share on other sites

you cant have the function in the while loop do it like this

 

function splitByCaps($string){
  return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string);
}
while(odbc_fetch_row($sqlresult_products))
{
  echo "<tr>";
  echo "<td><font size=1 face=verdana><b>";
  $data = substr($phpresult_vendors,9,-9);
  echo splitByCaps($data);
}

EDIT: just echoed out the value instead of assigning to another variable to suit your script better

Link to comment
Share on other sites

Hmm. Since you can use array reference to check a letter, I thought it would work, but apparently it doesn't. This should work:

$string = 'ThisText';

$newstring = '';
foreach( str_split( $string ) AS $letter ) {
    $newstring .= ( ctype_upper( $letter ) ? ' ' : '' ) . $letter;
}
$string = trim( $newstring );

Link to comment
Share on other sites

Personally, I think rythemton's method is better...

 

 

Regardless, these forums aren't meant for code to be written for you only to jump back with the error.  Research these errors.  This isn't a race to see who can write the perfect code either.  Just my 2 cents.

 

Link to comment
Share on other sites

I don't believe it was a race, if you look the code was posted only 30secs apart at the start, the OP had attempted to implement the code and we had both corrected the issues he had in our examples, I personally prefer a function over a loop but that is only my preference and I am not saying either way is better, they both reach the same result.

Link to comment
Share on other sites

apologies Zane if I offended you, or rythemton and dragon_sa. I did not mean to use and abuse if that is what was seen.

I only asked for some assistance with php and how to do a split in a string.

I also don't expect this to be a race or method comparison. but I have tried to understand the errors and I am having difficulty.

 

rythemton, your solution worked like a charm, I could kiss you.

Link to comment
Share on other sites

dragon_sa, your code inserts a space at the beginning of the string when the first character is uppercase.

 

I would simplify the regular express to something like /\B(?=[A-Z])/ and make the replacement just a space character.

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.