I have:
$text = "1235-text1-text2-a1-780-c-text3";
How can I get this with preg_replace? It is necessary to redirect 301.
"text1-text2-a1-780-c-text3"
for no use regex you can try
trim(strstr($text, '-'),'-');
No Regex needed:
$result = substr($text, strpos($text, '-')+1);
Or:
$result = trim(strstr($text, '-'), '-');
This will work
[^-]*-
Regex Demo
PHP Code
$re = "/[^-]*-/";
$text = "1235-text1-text2-a1-780-c-text3";
$result = preg_replace($re, "", $text, 1);
Ideone Demo
Or use preg_match
<?php
$text = "1235-text1-text2-a1-780-c-text3";
preg_match("%[^-]*-(.*)%",$text, $matchs);
var_dump($matchs[1]);
// Output "text1-text2-a1-780-c-text3"
?>
As you wanted, using preg_replace:
$re = '/^([\w]*-)/';
$str = "1235-text1-text2-a1-780-c-text3";
$match = preg_replace($re, "", $str);
var_dump($match);
An alternative using preg_match:
$re = '/-(.*)/';
$str = "1235-text1-text2-a1-780-c-text3";
preg_match($re,$str,$matches);
var_dump($matches[1]);
Related
I want to remove the first and last word from a string.
$string = "12121";
I tried trimming it like
$string = "12121";
$trimmed = trim($string, 1);
print($trimmed);
Result
22
And I want
212
So please help me
You can try Regular expressions
$string = "12121";
$trimmed = preg_replace('(^.)', '', $string);
$trimmed = preg_replace('(.$)', '', $trimmed);
print($trimmed);
but it seems overkill to use regex in this
so substr(like #aqib mentioned) might be the one you're looking for
$string = "12121";
$trimmed = substr($string, 1, -1);
print($trimmed);
can you try this?
<!DOCTYPE html>
<html>
<body>
<?php
$string = "12121";
$temp = ltrim($string, 1);
$trimmed = rtrim($temp, 1);
print($trimmed);
?>
</body>
</html>
I have make a try like this:
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-13);
and the output work :
localhost/product/-/123456 cause this just for above link with 13 character after /-/123456
How to remove all? i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-(.*));
not work and error sintax.
and i try
$string = "localhost/product/-/123456-Ebook-Guitar";
echo $string = substr($string, 0, strpos(strrev($string), "-/(0-9+)")-999);
the output is empty..
Assume there are no number after localhost/product/-/123456, then I will just trim it with below
$string = "localhost/product/-/123456-Ebook-Guitar";
echo rtrim($string, "a..zA..Z-"); // localhost/product/-/123456
Another non-regex version, but require 5.3.0+
$str = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo dirname($str) . "/" . strstr(basename($str), "-", true); //localhost/product/-/123456
Heres a more flexibility way but involve in regex
$string = "localhost/product/-/123456-Ebook-Guitar";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
$string = "localhost/product/-/123456-Ebook-Guitar-1-pdf/";
echo preg_replace("/^([^?]*-\/\d+)([^?]*)/", "$1", $string);
// localhost/product/-/123456
This should match capture everything up to the number and remove everything afterward
regex101: localhost/product/-/123456-Ebook-Guitar
regex101: localhost/product/-/123456-Ebook-Guitar-1-pdf/
Not a one-liner, but this will do the trick:
$string = "localhost/product/-/123456-Ebook-Guitar";
// explode by "/"
$array1 = explode('/', $string);
// take the last element
$last = array_pop($array1);
// explode by "-"
$array2 = explode('-', $last);
// and finally, concatenate only what we want
$result = implode('/', $array1) . '/' . $array2[0];
// $result ---> "localhost/product/-/123456"
Here is my character
/public/proj/index.php/home
/public/proj/index.php/test
/public/proj/home
/public/proj/test
I just need to replace the character after the right most '/' to login. So that it will be
/public/proj/index.php/login
/public/proj/index.php/login
/public/proj/login
/public/proj/login
Try this -
$str = "/public/proj/index.php/home";
$vals = explode('/', $str);
$vals[count($vals)-1] = 'login';
$str = implode('/', $vals);
var_dump($str);
This should work for you:
$str = "/public/proj/home";
echo $str = substr_replace($str, "login", -1*(strlen(basename($str))), strlen(basename($str)));
Output:
/public/proj/login
how to replace <p>hello</p> <p>world</p> to hello<br />world <br />
I've tried searching on stack but there is no matched result.
You could do this by using str_replace() function.
For instance:
$string = "<p>hello</p> <p>world</p>";
$string = str_replace('<p>', '', $string);
$string = str_replace('</p>', '<br />' , $string);
I try this myself and get what I expected
$pattern = '/<p>(\w+)\<\/p>/';
$subject = '<p>hello</p><p>world</p>';
$replacement = '${1}<br/>';
$out = preg_replace($pattern, $replacement, $subject);
I just wonder which is better regex or str_replace
I wrote a better solution, hope everybody can see it helpful and maybe improve it
$pattern = '/<p(.*?)>((.*?)+)\<\/p>/';
$replacement = '${2}<br/>';
$subject = 'html string';
$out = preg_replace($pattern, $replacement, $subject);
Use this to prevent breaking the first and last <p></p> :
$string = str_replace($string, '</p><p>', '');
But if a space comes between the tags, it won't work.
<?php
$str = '<p>hello</p> <p>world</p>';
$replaceArr = array('<p>', '</p>', '</p> <p>');
$replacementArr = array('', '', '<br />');
$str = str_replace($replaceArr, $replacementArr, $str);
echo $str;
?>
Try above code.
I wan't to replace
[a href='url']link[/a]
to
<a href='url'>link</a>
I am using $line = str_replace("[a href='+(.*)+']", "<a href='+(.*)+' >", $line); is not working.
Why not just use:
$search = array('[', ']');
$replace = array('<', '>');
$line = str_replace($search, $replace, $line);
You have to use a regular expression to do this
$line = preg_replace('~\\[a +href=\'([^\']+)\'\\]([^\\[]+)\\[/a\\]~', '$2', $line);
simply use
$string = str_replace(array('[', ']'), array('<', '>'), $string);
This is a great tutorial http://www.youtube.com/watch?v=x9VLWlQhNtM it shows you how to make a small templating engine and it covers what your asking
Try this :
$str = "[a href='url']link[/a]";
$new_str = preg_replace('/\[a href=\'(.*)\'\](.*)\[\/a\]/','<a href=\'$1\'>$2</a>',$str);
echo $new_str;