Remove spaces from the beginning and end of a string - php

I am pretty new to regular expressions.
I need to clean up a search string from spaces at the beginning and the end.
Example: " search string "
Result: "search string"
I have a pattern that works as a javascript solution but I cant get it to work on PHP using preg_replace:
Javascript patern that works:
/^[\s]*(.*?)[\s]*$/ig
My example:
$string = preg_replace( '/^[\s]*(.*?)[\s]*$/si', '', " search string " );
print $string; //returns nothing
On parse it tells me that g is not recognized so I had to remove it and change the ig to si.

If it's only white-space, why not just use trim()?

Yep, you should use trim() I guess.. But if you really want that regex, here it is:
((?=^)(\s*))|((\s*)(?>$))

Related

How do I use preg replace to remove this?

I have a string $test='23487°';
How can I remove all the instances of the little circle that appears in the string using preg replace?
What to I enter for the regex to remove it?
EDIT - as Pekka says, str_replace is better I am now using that. But the little circle is still not recognized by PHP...
You don't need regex, just str_replace:
$test = str_replace('°', '', $test);
The first parameter is the search term – the bit that will be found. The second parameter is the replacement string – the text that will be inserted instead. A blank string means "replace it with nothing", i.e. "remove it". The third parameter is the string on which to operate.
try with:
$test = preg_replace('/[^(\x20-\x7F)]*/','', $test);
this will replace all your non ascii characters from your string.
If you want to use preg_replace, you can do it like this:
$test = preg_replace('[°]', '', $test);
Also, for reference, here is a great site to test your regex:
http://www.solmetra.com/scripts/regex/index.php

PHP Regex moving selection to different location in string

I currently have this regex:
$text = preg_replace("#<sup>(?:(?!</?sup).)*$key(?:(?!</?sup).)*<\/sup>#is", '<sup>'.$val.'</sup>', $text);
The objective of the regex is to take <sup>[stuff here]$key[stuff here]</sup> and remove the stuff within the [stuff here] locations.
What I actually would like to do, is not remove $key[stuff here]</sup>, but simply move the stuff to $key</sup>[stuff here]
I've tried using $1-$4 and \\1-\\4 and I can't seem to get the text to be added after </sup>
Try this;
$text = preg_replace(
'#<sup>((?:(?!</?sup).)*)'.$key.'((?:(?!</?sup).)*)</sup>#is',
'<sup>'.$val.'</sup>\1\2',
$text
);
The (?:...)* bit isn't actually a sub-pattern, and is therefor not available using backreferences. Also, if you use ' rather than " for string literals, you will only need to escape \ and '
// Cheers, Morten
You have to combine preg_match(); and preg_replace();
You match the desired stuff with preg_match() and store in to the variable.
You replace with the same regex to empty string.
Append the variable you store to at the end.

Remove part of a string with regex

I'm trying to strip part of a string (which happens to be a url) with Regex. I'm getting better out regex but can't figure out how to tell it that content before or after the string is optional. Here is what I have
$string='http://www.example.com/username?refid=22';
$new_string= preg_replace('/[/?refid=0-9]+/', '', $string);
echo $new_string;
I'm trying to remove the ?refid=22 part to get http://www.example.com/username
Ideas?
EDIT
I think I need to use Regex instead of explode becuase sometimes the url looks like http://example.com/profile.php?id=9999&refid=22 In this case I also want to remove the refid but not get id=9999
parse_url() is good for parsing URLs :)
$string = 'http://www.example.com/username?refid=22';
$url = parse_url($string);
// Ditch the query.
unset($url['query']);
echo array_shift($url) . '://' . implode($url);
CodePad.
Output
http://www.example.com/username
If you only wanted to remove that specific GET param, do this...
parse_str($url['query'], $get);
unset($get['refid']);
$url['query'] = http_build_query($get);
CodePad.
Output
http://example.com/profile.php?id=9999
If you have the extension, you can rebuild the URL with http_build_url().
Otherwise you can make assumptions about username/password/port and build it yourself.
Update
Just for fun, here is the correction for your regular expression.
preg_replace('/\?refid=\d+\z/', '', $string);
[] is a character class. You were trying to put a specific order of characters in there.
\ is the escape character, not /.
\d is a short version of the character class [0-9].
I put the last character anchor (\z) there because it appears it will always be at the end of your string. If not, remove it.
Dont use regexs if you dont have to
echo current( explode( '?', $string ) );

How to remove all links from a mixed string with php

i have a variable in php can be like this : $string = "hello check out this http://xx.xx/xxx & thanks !!";
i want a function to strip that link and remove it from the string, the url can be with WWW. or without it.
also, this variable can contains multiple urls .
Thanks
You could use regular expressions to do it:
$string = preg_replace('\b(https?|ftp|file)://[-A-Z0-9+&##/%?=~_|!:,.;]*[-A-Z0-9+&##/%=~_|]', '', $string);
That will find every instance of a URL in the string and replace it with a blank string.
You can use a regular expression to match and replace all URL formats, as follows:
$string = preg_replace("/(^¦\s)(http:\/\/)?(www\.)?[\.0-9a-zA-Z\-_~]+\.(com¦net¦org¦info¦name¦biz¦.+\.\w\w)((\/)?[0-9a-zA-Z\.\-_~#]+)?\b/ie","",$string);

Trimming A Vertical Bar

In PHP the trim function has a parameter for trimming specific characters (handy for leading zeros and the like). I can't seem to get it to accept a vertical bar (|) character. Anyone know how to get this working? I tried the hex value but had no luck. I'm sure it's something simple.
Cheers
It works for me:
var_dump(trim('|foo|', '|')); // string 'foo' (length=3)
Maybe you have some whitespace around it, or your're using the wrong pipe character? ¦ vs |
Works for me:
$str = "|test string";
echo trim($str, "|");
test string
Can you show some of your code?
Maybe you want to remove a | in the middle of a string
you can use str_replace
str_replace("|", "", $str);
echo trim('|text|', '|'); // returns text
The second param was added in PHP 4.1!
trim() only removes characters from the beginning and end of a string. If you'd like to replace characters in the middle of a string, use str_replace(), or preg_replace() if you like regular expressions.

Categories