URL Friendly Username in PHP? - php

On my PHP site, currently users login with an email address and a password. I would like to add a username as well, this username they g\set will be unique and they cannot change it. I am wondering how I can make this name have no spaces in it and work in a URL so I can use there username to link to there profiles and other stuff. If there is a space in there username then it should add an underscore jason_davis. I am not sure the best way to do this?

function Slug($string)
{
// convert to entities
$string = htmlentities( $string, ENT_QUOTES, 'UTF-8' );
// regex to convert accented chars into their closest a-z ASCII equivelent
$string = preg_replace( '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $string );
// convert back from entities
$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
// any straggling caracters that are not strict alphanumeric are replaced with a dash
$string = preg_replace( '~[^0-9a-z]+~i', '-', $string );
// trim / cleanup / all lowercase
$string = trim( $string, '-' );
$string = strtolower( $string );
return $string;
}
$user = 'Alix Axel';
echo Slug($user); // alix-axel
$user = 'Álix Ãxel';
echo Slug($user); // alix-axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel

In other words... you need to create a username slug. Doctrine (ORM for PHP) has a nice function to do it. Doctrine_Inflector::urlize()
EDIT: You should also keep username slug in database, as a Unique Key column. Then every search operation should be done based on that column, not original username.

Related

Steam API str_replace htmlspecialchars

i have been making a Steam API. However i need remove htmlspecialchars from Steam Name when they login.
This is what i have done so far:
Get user name from database:
$name = fetchinfo("name","users","steamid",$steamid);
When they login it should str_replace his name:
$name = str_replace("**something here**",'',$name);
How can i add htmlspecialchars for that?
It's easy enough to run both str_replace and htmlspecialchars on your string through nesting:
$name = "Tony & Sarah";
$name = htmlspecialchars( str_replace(' ','-',$name) );
// $name == "Tony-&-Sarah"
If this doesn't match what you're looking for, an example of what you're trying to do with str_replace would be helpful.
Use preg_replace() for this:
function replaceSpecialChars($string, $chars){
$c = implode('\\', str_split($chars));
return preg_replace('/['.$c.']/', '', $string);
}
$test = replaceSpecialChars('a##bcde %8&)`', '`~!##$%^&*()');

PHP preg_replace url get parameters in string

Thanks to #s.d.a.p.e I've come a step close but I'm not quite there yet.
What I'm trying to do is replace all instances of a string in a block of text. I want to replace something like this:
user is ?user_id=34&first_name=Ralph so is ?user_id=1 also
With this:
user is /user/34/ so is /user/1/ also
Here is the preg_replace code I'm using:
$pattern = '#\?user_id=([0-9]+)#';
$replace = '/user/$1/';
echo preg_replace($pattern,$replace,$string);
With that pattern I end up with this:
user is /user/34/&first_name=Ralph so is /user/1/ also
Thanks again.
try this:
$string = "user is ?user_id=34&first_name=Ralph so is ?user_id=1 also";
$result = preg_replace('/\?(user)_id=(\d+)(.*?)(?! )/i', '/$1/$2/$3', $string );
echo $result ;
Output:
user is /user/34/&first_name=Ralph so is /user/1/ also
DEMO
I'd use this:
$string = 'user is ?user_id=34&first_name=Ralph so is ?user_id=1 also';
$pattern = '#\?user_id=([0-9]+)\S*#';
$replace = '/user/$1/';
echo preg_replace($pattern, $replace, $string);
Where \S stands for any character that is not a space.
Output:
user is /user/34/ so is /user/1/ also
print preg_replace(
'#\?user_id=([0-9]+)\&(first_name=(?:.*))#',
'/user/$1?$2',
'?user_id=34&first_name=Ralph'
);
result :
/user/34?first_name=Ralph if get it right..

Extracting and removing URL from a block of text

I have this block of text:
$text = 'This just happened outside the store http://somedomain.com/2012/12/store there might be more text afterwards...';
It needs to be converted to:
$result['text_1'] = 'This just happened outside the store';
$result['text_2'] = 'there might be more text afterwards...';
$result['url'] = 'http://somedomain.com/2012/12/store';
This is my current code, it does detect the url, but i can only remove it from the text, I still need the url value separately in an array:
$string = preg_replace('/https?:\/\/[^\s"<>]+/', '', $text);
//returns "This just happened outside the store there might be more text afterwards..."
Any ideas? Thanks!
Temporal solution (can this be optimized?)
$text = 'This just happened outside the store http://somedomain.com/2012/12/store There might be more text afterwards...';
preg_match('/https?:\/\/[^\s"<>]+/',$text,$url);
$string = preg_split('/https?:\/\/[^\s"<>]+/', $text);
$text = preg_replace('/\s\s+/','. ',implode(' ',$string));
echo ''.$text.'';
Do you need it to store in a variable or just need it inside the ahref?
How about this?
<?php
$text = 'This just happened outside the store http://somedomain.com/2012/12/store There might be more text afterwards...';
$pattern = '#(.*?)(https?://.*?) (.*)#';
$ret = preg_replace( $pattern, '$3', $text );
var_dump( $ret );
$1, $2, and $3 corresponds to the 1st, 2nd, 3rd parenthesis
the output would be
There might be more text afterwards...
you could split your string on the regex using preg_split to give you an array
$result = preg_split('/(https?:\/\/[^\s"<>]+)/', $the_string, -1, PREG_SPLIT_DELIM_CAPTURE);
// $result[0] = preamble
// $result[1] = url
// $result[2] = possible afters

How do I remove A-Z from _POST

I have one form which will not return any error to user.
If user submit
$input = '~%$!#_)(*)*(!#_)(*&AB**23**CDEFGHIJKLMNdsf**234**OPQRSTUV**499**WXYZ'
I want script will remove all chars accept 0-1
$replace = '23234499';
Then convert automate to number_format or money_format
$output = '23,234,499.00';
Let me know
If no number found on input I want the output to be 0.00
You can do this:
$str = preg_replace("/[^0-9]+/", "", $your_string);
You can do:
$input = '~%$!#_)(*)*(!#_)(*&AB**23**CDEFGHIJKLMNdsf**234**OPQRSTUV**499**WXYZ';
$input = preg_replace('/\D/','',$input);
$input = number_format($input,2);
See it
Since the definition of \D might contain digits other than 0-9 depending on the locale. It is safer to use
$input = preg_replace('/[^0-9]/','',$input);

Unique slugs that, if fits with slug-1, slug-2... slug-n

I am using only slug to identify a page on the website, like: example.tld/view/this-fancy-slug . This is generated from the title automatically with this function:
public static function Normalize($str)
{
$charset = "UTF-8";
$separator = "-";
$str = strtolower(htmlentities($str, ENT_COMPAT, $charset));
$str = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $str);
$str = preg_replace('/([^a-z0-9]+)/', $separator, html_entity_decode($str, ENT_COMPAT, $charset));
$str = trim($str, $separator);
return $str;
}
This returns a perfect slug... but I need unique slugs. So I've to combine with mysql to check if exists a slug that fits with the created. No problem with that.
The problem is that I want to add a -1 at the final if there is ONE slug. But can be buggy if are added 3 equal slugs so... how can I manage this to go from slug, slug-1, slug-2, slug-3... slug-100, slug-n?
Thank you in advance!
Don't just check if that identical slug is present. Use a regular expression to count all the slugs that follow the pattern /(<base-regex-for-slug>)(-\d+)?/. Because you only allow alphanumerics your base regex above will be the slug itself.

Categories