MyBB Community Forums

Full Version: Weird PHP issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
$str = "Something here, Something else here, Something different here.";
$str = str_replace("Something", "<strong>Something</strong>", $str);

Outputs:

Something here, Something else here, Something different here.

However, if I just put a space at the start of the string, it works fine.

Why Sad
PHP version?

It works fine for me (5.3)
5.2.8.

What's even weirder is it only happens if the string starts with certain characters. For instance, if it starts with an R or S the above happens, it's fine if it starts with a J. So if it starts with Rainbow or Sandwich, the first one wouldn't be bold but the rest would, and if it starts with Jumper, they'd all be bold.

o.O
are you using Wamp??

upgrade it to 2.0i

and also the best command for string replacements is
/** | and | are the delimiters you can use also # and # **/
$str = "Something here, Something else here, Something different here.";
$str = preg_replace("|Something|", "<strong>Something</strong>", $str);
Heh why do you want it to say Rainbow and Sandwich? Are you having this with Wamp or something like that or at your webhost?
(2009-12-30, 03:39 AM)Zomaian Wrote: [ -> ]Heh why do you want it to say Rainbow and Sandwich? Are you having this with Wamp or something like that or at your webhost?

probably an old wamp version its still 5.2.8
Could be a server. I know my servers PHP is way older then this. A lot of admins have a good mindset.
File encoding?
(2009-12-30, 03:39 AM)Zomaian Wrote: [ -> ]Heh why do you want it to say Rainbow and Sandwich?

I don't, they're just examples Toungue

It's on a normal webhost, the same happens on my localhost with PHP 5.3.0, dunno about file encoding, never changed anything or done anything with encoding, not had an issue like this before.

I'll try a preg_match but I've never really understood how it works, best I learn though eh.

Edit: Just tried that, same issue with preg_replace. Ahh.

OK, maybe this makes a difference, it's actually searching for variables, so it's like this:

$array = explode(",", $string);
foreach($array as $val)
{
	$str = str_replace($val, "<strong>" . $val. "</strong>", $str);
	//or
	$str = preg_replace("|$val|", "<strong>" . $val. "</strong>", $str);
}

Same thing, replaces all the ones that aren't at the start of $str, and if it is at the start of $str, sometimes it does it, sometimes it doesn't.
Here's a temporary fixed function if you want.

function fixed_replace($delimiter, $replacement, $str)
{
       $str2 = " " . $str;
       $replaced = str_replace($delimiter, $replacement, $str2);
       return substr($replaced, 1);
} 
Pages: 1 2