livethedead Posted February 12, 2012 Share Posted February 12, 2012 I can't figure out why this isn't working. <?php $twitter = trim($_REQUEST['twitter_handle']); if (preg_match("/^@/", $twitter)) { preg_replace("/@/", "twitter.com/", $twitter); } else { $twitter = "twitter.com/" + $twitter; } <? if I enter @asdjf it just returns @asdjf. Appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/ Share on other sites More sharing options...
ragax Posted February 12, 2012 Share Posted February 12, 2012 Hi livethedead, It sounds like it could be an encoding problem, where what you see is not what you think it is. Before your preg_match, try running this to see the actual components of the string. If there is something odd, it should jump out. for($i=0;$i<strlen($twitter);$i++) echo "{{$twitter[$i]}}" . ord($twitter[$i]) . " <br />\n"; Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317363 Share on other sites More sharing options...
livethedead Posted February 12, 2012 Author Share Posted February 12, 2012 edit: I occidentally added a character, it's working now. {@}64 {p}112 {h}104 {p}112 @php The script isn't from pratical use, I'm just teaching myself PHP. After finishing a chapter in this book on regular expressions a snippet suggested to rewrite an old script using regex. I've been sitting here for about and hour self-proclaiming there is nothing wrong with this code haha. I would like to figure out what's wrong, for future reference. I appreciate your helping. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317368 Share on other sites More sharing options...
ragax Posted February 12, 2012 Share Posted February 12, 2012 Cool, glad to hear you figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317374 Share on other sites More sharing options...
livethedead Posted February 13, 2012 Author Share Posted February 13, 2012 Actually, I haven't lol. i have no idea what to do with those values. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317380 Share on other sites More sharing options...
AyKay47 Posted February 13, 2012 Share Posted February 13, 2012 Actually, I haven't lol. i have no idea what to do with those values. what do you mean? I don't quite understand whats going wrong for you now, can you explain the problem more thoroughly. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317392 Share on other sites More sharing options...
livethedead Posted February 13, 2012 Author Share Posted February 13, 2012 I'm still having the original problem, I used the code mentioned above but not sure exactly what to do with the results in the process of solving the problem. My edit in the second post is misleading, didn't realise that until just now, I was referring to the code he gave me. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317405 Share on other sites More sharing options...
ragax Posted February 13, 2012 Share Posted February 13, 2012 Sorry, I missed the original problem. Use $twitter=preg_replace( etc That will fix it. Here is a working example. <?php $twitter = '@whatever'; if (preg_match("/^@/", $twitter)) { $twitter=preg_replace('/@/','twitter.com/',$twitter); } else { $twitter = "twitter.com/".$twitter; } echo $twitter.'<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317410 Share on other sites More sharing options...
livethedead Posted February 13, 2012 Author Share Posted February 13, 2012 Ahhh I see what the problem was, thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317777 Share on other sites More sharing options...
ragax Posted February 13, 2012 Share Posted February 13, 2012 You're welcome, glad it works. And sorry for taking you in another direction originally, I too missed it on first glance. Quote Link to comment https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317835 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.