I have a big problem understanding how to use preg_replace.
I need to replace the string FROM:
GPCNT2(90)>GPBRL2(90)>GPDUT1(180)>GPJDPR TO: GPCNT2>GPBRL2>GPDUT1>GPJDPR
What regular expression should I use to accomplish this?
Current code:
if(strpos($route_path, '/(\d+)/') !== false) {
$route_path = preg_replace('/(\d+)/', '', $route_path);
echo "<br>" .$route_path."</br>";
}
Instead of strpos, use preg_match. You also need to escape the parenthesis \(
here you go:
<?php
$route_path = "GPCNT2(90)>GPBRL2(90)>GPDUT1(180)>GPJDPR";
if(preg_match('/\(\d+\)/', $route_path)) {
$route_path = preg_replace('/\(\d+\)/', '', $route_path);
echo "<br>" .$route_path."</br>";
}
//</br>GPCNT2>GPBRL2>GPDUT1>GPJDPR</br>
Ideone Demo
Is GPCNT2(90)>GPBRL2(90)>GPDUT1(180)>GPJDPR a String? If it is; then you may want to try something like this:
<?php
$str = "GPCNT2(90)>GPBRL2(90)>GPDUT1(180)>GPJDPR";
$strFiltered = preg_replace("#(\(\d*\))(>)#i", "$2", $str);
//COMPARE THE RESULTS TO SEE IF ALL IS OK AS YOU DESIRED IT...
var_dump($str);
var_dump($strFiltered);
Related
I want to display only last string value from string. This is my string ShopTop205/12.50R15
I want to just display this type of string
205/12.50R15
I have tried like
<?php
$catName= 'ShopTop205/12.50R15';
echo substr($catName, strrpos($catName, ' ') + 1);
?>
second way
<?php
$string = 'ShopTop205/12.50R15';
$string = explode('', $string);
$last_string = end($string);
echo $last_string;
?>
I have used substr() function also but i could not get result that i want.
how could i do this ?
You may remove the initial non-numeric chars with a regex:
$catName= 'ShopTop205/12.50R15';
$res = preg_replace('~^\D+~', '', $catName);
echo $res; // => 205/12.50R15
See the PHP demo
The pattern is ^\D+ here, and it matches any one or more (+) chars other than digits (\D) at the start of the string (^).
See the regex demo.
$catName= 'ShopTop205/12.50R15';
$result = substr($catName, 7,20);
print $result;//205/12.50R15;
Check this one
$catName= 'ShopTop205/12.50R15';
preg_match('/^\D*(?=\d)/', $catName, $m);
$pos = isset($m[0]) ? strlen($m[0]) : false;
$text = substr($catName,$pos); // this will contain 205/12.50R15
Doing it with substr() given that the length is always the same:
https://ideone.com/A4Avpt
<?php
echo substr('ShopTop205/12.50R15', -12);
?>
Output: 205/12.50R15
I want to make some changes to the html but I have to follow certain rules.
I have a source code like this;
A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli
I need to convert this into the following;
A beautiful sentence http://test.google.com/, You can reach here http://www.google.com/test-mi or http://test.google.com/aliveli
I tried using str_replace;
$html = str_replace('://www.google.com/test','://test.google.com');
When I use it like this, I get an incorrect result like;
A beautiful sentence http://test.google.com/, You can reach here http://test.google.com/-mi or http://test.google.com/aliveli
Wrong replace: http://test.google.com/-mi
How can I do this with preg_replace?
With regex you can use a word boundary and a lookahead to prevent replacing at -
$pattern = '~://www\.google\.com/test\b(?!-)~';
$html = preg_replace($pattern, "://test.google.com", $html);
Here is a regex demo at regex101 and a php demo at eval.in
Be aware, that you need to escape certain characters by a backslash from it's special meaning to match them literally when using regex.
It seems you're replacing the subdirectory test to subdomain. Your case seems to be too complicated. But I've given my best to apply some logic which may be reliable or may not be unless your string stays with the same structure. But you can give a try with this code:
$html = "A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli";
function set_subdomain_string($html, $subdomain_word) {
$html = explode(' ', $html);
foreach($html as &$value) {
$parse_html = parse_url($value);
if(count($parse_html) > 1) {
$path = preg_replace('/[^0-9a-zA-Z\/-_]/', '', $parse_html['path']);
preg_match('/[^0-9a-zA-Z\/-_]/', $parse_html['path'], $match);
if(preg_match_all('/(test$|test\/)/', $path)) {
$path = preg_replace('/(test$|test\/)/', '', $path);
$host = preg_replace('/www/', 'test', $parse_html['host']);
$parse_html['host'] = $host;
if(!empty($match)) {
$parse_html['path'] = $path . $match[0];
} else {
$parse_html['path'] = $path;
}
unset($parse_html['scheme']);
$url_string = "http://" . implode('', $parse_html);
$value = $url_string;
}
}
unset($value);
}
$html = implode(' ', $html);
return $html;
}
echo "<p>{$html}</p>";
$modified_html = set_subdomain_string($html, 'test');
echo "<p>{$modified_html}</p>";
Hope it helps.
If the sentence is the only case in your problem you don't need to start struggling with preg_replace.
Just change your str_replace() functioin call to the following(with the ',' at the end of search string section):
$html = str_replace('://www.google.com/test,','://test.google.com/,');
This matches the first occurance of desired search parameter, and for the last one in your target sentence, add this(Note the '/' at the end):
$html = str_replace('://www.google.com/test/','://test.google.com/');
update:
Use these two:
$targetStr = preg_replace("/:\/\/www.google.com\/test[\s\/]/", "://test.google.com/", $targetStr);
It will match against all but the ones with comma at the end. For those, use you sould use the following:
$targetStr = preg_replace("/:\/\/www.google.com\/test,/", "://test.google.com/,", $targetStr);
This should be simple. I want to change all of these substrings:
\\somedrive\some\path
into
file://\\somedrive\some\path
but if substrings already have a file:// then I don't want to append it again.
This doesn't seem to do anything:
var_export( str_replace( '\\\\', 'file://\\\\', '\\somedrive\some\path file://\\somedrive\some\path' ) );
What am I doing wrong? Also, the above doesn't take into test for file:// already being there; what's the best way of dealing with this?
UPDATE test input:
$test = '
file://\\someserver\some\path
\\someotherserver\path
';
test output:
file://\\someserver\some\path
file://\\someotherserver\path
Thanks.
You should consider escape sequence in string also.
if((strpos($YOUR_STR, '\\\\') !== false) && (strpos($YOUR_STR, 'file://\\\\') === false))
var_export( str_replace( '\\\\', 'file://\\\\', $YOUR_STR ) );
Use a regular expression to check if the given substring starts with file://. If it does, don't do anything. If it doesn't, append file:// at the beginning of the string:
if (!preg_match("~^file://~i", $str)) {
$str = 'file://' . $str;
}
As a function:
function convertPath($path) {
if (!preg_match("~^file://~i", $path)) {
return 'file://'.$path;
}
return $path;
}
Test cases:
echo convertPath('\\somedrive\some\path');
echo convertPath('file://\\somedrive\some\path');
Output:
file://\somedrive\some\path
file://\somedrive\some\path
EDIT
For multiple occurrences : preg_replace('#((?!file://))\\\\#', '$1file://\\\\', $path)
This will work to give you the output you are expecting. As php.net says double slash will be converted into single slash.
if (!preg_match('/^file:\/\//', $str)) {
$str = "file://\\".stripslashes(addslashes($str));
}
Please try this:
$string = "\\somedrive\some\path";
$string = "\\".$string;
echo str_replace( '\\\\', 'file://\\\\',$string);
I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);
I want to get all Performance ID's from this page .
<?php
$content = file_get_contents("http://www124.popmundo.com/Common/Performances.asp?action=ComingPerformances&ArtistID=1962457");
$regex = "Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/s";
//all pattern variations tested, not working
if(preg_match_all($regex, $content, $m))
print_r($m);
else
echo "FALSE";
// this is returning FALSE
Use & instead of & in your regex.
Try this:
$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
It looks like an escape problem. Not knowing php, I would guess one of these
might fix it:
$regex = 'Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)';
or
$regex = "Performances\\.asp\\?action=Arrangements&PerformanceID=([0-9]+)";
or
$regex = '/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/';