Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/256971-preg_match/
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317363
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317368
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317405
Share on other sites

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 />';
?>

Link to comment
https://forums.phpfreaks.com/topic/256971-preg_match/#findComment-1317410
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.