Jump to content

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


Recommended Posts

[color=red]Replace your single quotes when double ones and you'll have your answer...Problem solved. God I'm stupid.[/color]

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).
Link to comment
Share on other sites

When you enclose a variable name in single quotes, it is not expanded to it's value, so your line
[code]<?php $NUMBA = substr_count($var, '$name'); ?>[/code] 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 [code]<?php $NUMBA = substr_count($var, $name); ?>[/code]

Ken
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.