I would like to replace ":" with "\xEF\xBC\x9A" however, I do not want to do this for : that follows "http", "https", and string with the format of #[{numbers}:{numbers}:{some text}].
What I have doesn't really work as (?<!#\[\d) does not check what's after it. My current implementation only work for something like #[1:2:text]. Thanks!
$string=preg_replace('#(?<!https)(?<!http)(?<!#\[\d)(?<!#\[\d:\d):#', "\xEF\xBC\x9A", $string);
Try this:
preg_replace('/(#\[\d+:\d+:[^]]*]|https?:)|:/e', '"$1"?"$1":"\xEF\xBC\x9A"', $string);
Try this regex:
(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):
It should match the first and second instances of ':' here.
: test: http: #[1:2:text]
Usage Sample:
$string = preg_replace('/(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):/', '\xEF\xBC\x9A', $string);
Related
I have a string http://localhost:9000/category that I want to replace with category.html, i.e. strip everything before /category and add .html.
But can't find a way to do this with str_replace.
You want to use parse_url in this case:
$parts = parse_url($url);
$file = $parts['path'].'.html';
Or something along that line. Experiment a bit with it.
Ismael Miguel suggested this shorter version, and I like it:
$file = parse_url($url,PHP_URL_PATH).'.html';
Much better than a ^*!$(\*)+ regular expression.
.*\/(\S+)
Try this.Replace by $1.html.see demo .
http://regex101.com/r/nA6hN9/43
Use preg_replace instead of str_replace
Regex:
.*\/(.+)
Replacement string:
$1.html
DEMO
$input = "http://localhost:9000/category";
echo preg_replace("~.*/(.+)~", '$1.html', $input)
Output:
category.html
A solution without regex:
<?php
$url = 'http://localhost:9000/category';
echo #end(explode('/',$url)).'.html';
?>
This splits the string and gets the last part, and appends .html.
Note that this won't work if the input ends with / (e.g.: $url = 'http://localhost:9000/category/';)
Also note that this relies on non-standard behavior and can be easily changed, this was just made as a one-liner. You can make $parts=explode([...]); echo end($parts).'.html'; instead.
If the input ends with / occasionally, we can do like this, to avoid problems:
<?php
$url = 'http://localhost:9000/category/';
echo #end(explode('/',rtrim($url,'/'))).'.html';
?>
So let's say we have a text like "CS x1CD x2CE x3"
What I want to do is replace each x1 with x1\n, x2 with x2\n, x3 with x3\n, etc. During runtime it is not known before hand what those values will be, but basically after each x[0-9]+ we want to append a newline.
How would I do this? I tried using regex such as
preg_replace("/x[0-9]+/", "${1}\n", $request['item_name']);
But that doesn't work as it just uses the first result.
You can avoid to refer to a capturing group with \K:
$result = preg_replace("/x[0-9]+\K/", "\n", $request['item_name']);
\K removes all on the left from match result.
If your server is running on a linux/unix system, you can skip a good part of the curly/double/single quotes/newline problem using PHP_EOL:
$result = preg_replace("/x[0-9]+/", '$0' . PHP_EOL, $request['item_name']);
You are looking for the following instead.
preg_replace('/x[0-9]+/', '${0}' . "\n", $request['item_name']);
See live demo.
Working Live Demo
Try this:
preg_replace("/(x[0-9]+)/","${0}\n",$request['item_name']);
Try this:
<?php
$string = $request['item_name'];
$string = preg_replace('/(x\d+)/', '$1\n', $string);
echo $string;
?>
DEMO
http://ideone.com/quh4M1
How do I remove parts just from a word in a string and leave intact the rest of it
Having the following situation
Notebooks_Lenovo and Tablets
I would like to remove the part _Lenovo and get back as result
Notebooks and Tablets
I know as option I have Regex but is there a php function which would do the job
Try This
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
Try str_replace() like,
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
the str_replace() function can help you
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
$string = str_replace("_Lenovo ", "", "Notebooks_Lenovo and Tablets");
echo $string;
I want to replace this URI
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi
by this URI
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&kljklj=sdfsd
==> I want to delete "&brand=Sony"
I tried this :
preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi');
but it doesn't work in a specific case : the case where the parameter "toto" in the URI doesn't exist
So if I do
preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony');
It doesn't work ==> "&brand=Sony" still appear
So how can I do ?
I wouldn't use regular expressions.
First, use parse_url to split the url into its bits and pieces.
Then, use parse_str on the query portion.
Do whatever you want to the query keys, then combine it all back.
To build the query string back: http_build_query
Then build the url using http_build_url
preg_replace("/\&brand=Sony/", "", $uri);
How about:
preg_replace('/[\?&]brand=\w*/', '', $url);
If you want the value of key brand
preg_replace('/&?brand=[^&]+/i','',$url);
(^.*)(&brand=.*)(&.*)?
you can just add ?:
(^.*)(&brand=.*)(&.*)?
echo preg_replace(
'/(^.*)(&brand=.*)(&.*)?/',
'$1$3',
'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony');
output:
http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0
Thank you everybody. So here is my final solution and it works fine :
<?php
$actual_link = 'index.php?'.$_SERVER['QUERY_STRING']; //complete link of my page
$parsedURL= parse_url($actual_link);
parse_str($parsedURL["query"],$tabParametersQuery);
$tabParametersQuery['brand']="";
$newURL = "index.php?".http_build_query($tabParametersQuery);
?>
When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"