Jump to content

Weird explode() error


ProNoob13

Recommended Posts

So, I wrote a little function the other day to fetch an array of elements out of character-separated-data (like "value1|value2|value3", but then fetched into an array using explode()). The only thing is just that it isn't working at all[/]. Every time I run the function, and catch it as an variable to use it as an array, it simply outputs the word "media", with each character separated into one array-element. Could anybody figure out what's wrong with my code? (Aside from my style, I know it's horrible. I'm halfway learning OOP) Any help would be greatly appreciated.

 

if(!function_exists('getText')) {
	function getText($item) {
		$text = explode(" | ", $data["text"][$item]);
		$text = Array(0=>"")+$text;

		return Array(1=>$data["text"][$item]); //outputs Array(0=>"", "m", "e", "d", "i", "a")
	}
}

17852_.zip

Link to comment
Share on other sites

This works for me:

<?php
if(!function_exists('getMyText')) {
function getMyText($item) {
  $text = explode(" | ", $item); 
  return $text; 
  //outputs array(4) { [0]=> string(5) "hello" [1]=> string(3) "out" [2]=> string(5) "there" [3]=> string( "everyone" }  
  } 
  }
$m = getMyText("hello | out | there | everyone");

var_dump($m);
?>

 

I changed the function name because getText is already a PHP function name : http://uk3.php.net/manual/en/function.gettext.php whych is why I assume you are using the if(!function_exists(...)).  Please try to avoid any atempts to overwrite php stored functions at runtime, nothing good will come from it.

Link to comment
Share on other sites

This works for me:

<?php
if(!function_exists('getMyText')) {
function getMyText($item) {
  $text = explode(" | ", $item); 
  return $text; 
  //outputs array(4) { [0]=> string(5) "hello" [1]=> string(3) "out" [2]=> string(5) "there" [3]=> string( "everyone" }  
  } 
  }
$m = getMyText("hello | out | there | everyone");

var_dump($m);
?>

 

I changed the function name because getText is already a PHP function name : http://uk3.php.net/manual/en/function.gettext.php whych is why I assume you are using the if(!function_exists(...)).  Please try to avoid any atempts to overwrite php stored functions at runtime, nothing good will come from it.

 

That has done the trick. Thank you so much! I didn't notice that the function already existed, and, indeed, the function_exists was a override for the error it threw up when I tried to use getText as my own function. Still, thanks a bunch!

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.