Jump to content


[SOVLED] This Always Happens, but I don't Know Why...

  • Please log in to reply
3 replies to this topic

#1 WinnieThePujols

  • Members
  • PipPip
  • Member
  • 21 posts

Posted 07 July 2006 - 09:51 PM

Replace your single quotes when double ones and you'll have your answer...Problem solved. God I'm stupid.

I'm coding a baseball site. I want my users to be able to have a custom tag called player. Basically, they'll type something like [player]His Name[/player], and the two tags will be replaced to a link; a query to the database will be made, his player ID will be found and the correct link will be added.

However, for some reason, I always seem to run into this problem when doing a str_replace...

Check this out:
$name = "[player]" . $first . " " . $last . "[/player]";
When I print that, I get: "[player]Scott Rolen[/player]" (minus the quotes); a strlen tells me it's 28 characters.

If I try and substr_count($var, '$name'), I get nothing. However, if I go:
substr_count($var, '[player]Scott Rolen[/player]'), it "works".

For the heck of it, I did:
$test = "[player]Scott Rolen[/player]";
$code = strlen($test);
print "<br>$code";

Not surprisingly, that prints out to be 28 characters -- the same as the other one.

The original string is:
this is a test [player]Albert Pujols[/player] and there is also [player]Scott Rolen[/player] and there is also [player]Jim Edmonds[/player]

So can someone please explain THIS:
$name = "[player]" . $first . " " . $last . "[/player]"; // printed it equals "[player]Scott Rolen[/player]"
$NUMBA = substr_count($var, '$name'); // printing $NUMBA results in a gooseegg (0)
$OTHER = substr_count($var, '[player]Scott Rolen[/player]'); // printing $OTHER results in 1...

This has to be some common problem or something... help!

What am I doing wrong?

PS: it's probably a typo if something doesn't matchup as the same... I'm 99% these are the exact same strings (or at least LOOK the exact same when printed out).

#2 WinnieThePujols

  • Members
  • PipPip
  • Member
  • 21 posts

Posted 07 July 2006 - 09:56 PM

OK, I've been messing around and...
if($name == "[player]Scott Rolen[/player]")
    print "Same";
    print "Not";

...that returns "Same.."

#3 redarrow

  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 09:58 PM

try strlen


echo strlen($name);

Wish i new all about php DAM i will have to learn

#4 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 July 2006 - 10:00 PM

When you enclose a variable name in single quotes, it is not expanded to it's value, so your line
<?php $NUMBA = substr_count($var, '$name'); ?>
is looking for the value of $var in the string $name, not the value of $name. In this instance you don't need and quotes, so write this line as
<?php $NUMBA = substr_count($var, $name); ?>


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users