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
https://forums.phpfreaks.com/topic/259499-weird-explode-error/
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
https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330226
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
https://forums.phpfreaks.com/topic/259499-weird-explode-error/#findComment-1330229
Share on other sites

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.