WinnieThePujols Posted July 7, 2006 Share Posted July 7, 2006 [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). Quote Link to comment https://forums.phpfreaks.com/topic/13983-sovled-this-always-happens-but-i-dont-know-why/ Share on other sites More sharing options...
WinnieThePujols Posted July 7, 2006 Author Share Posted July 7, 2006 OK, I've been messing around and...if($name == "[player]Scott Rolen[/player]"){ print "Same";}else{ print "Not";}...that returns "Same.." Quote Link to comment https://forums.phpfreaks.com/topic/13983-sovled-this-always-happens-but-i-dont-know-why/#findComment-54564 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 try strlen<?$name="redarrow";echo strlen($name);?> Quote Link to comment https://forums.phpfreaks.com/topic/13983-sovled-this-always-happens-but-i-dont-know-why/#findComment-54565 Share on other sites More sharing options...
kenrbnsn Posted July 7, 2006 Share Posted July 7, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13983-sovled-this-always-happens-but-i-dont-know-why/#findComment-54567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.