I want to remove all punctuation from beginning and end of a string except hyphen,underscore.
Example:if input is spice-b32. Or lg_b32; Then string after using preg_replace(); should be: spice-b32 and lg_b32;
i'm also tried to use preg_match('/^[A-Za-z0-9]/',$inm) for data validation use $inm=preg_replace('/^\PL+|\PL\z/','',$inm); but, when input a!-read_ result is a!-read
but output should be: a-read
if this preg_replace() OR preg_match() is not correct,then plz help..
If I understand correctly what you want, then something like this will do for you:
$inm=preg_replace('/[,.!?]*([-_]+)[,.!?]*/',
'\1',
preg_replace('/\b[.,?!]+|[.,!?]+\b/', '', $inm);
Feel free to add other characters that need to be stripped off to the character groups.
How about
$arr = array('spice-b32.', 'lg_b32;', 'a!-read_');
foreach ($arr as $str) {
echo preg_replace('/^[^\P{P}_-]+|[^\P{P}_-]+$/u', '', $str),"\n";
}
This will remove all punctuation (except _ and -) from the begining or end of a string.
output:
spice-b32
lg_b32
a!-read_
Related
I have an array with values like
$arr = array("abc-xyz","a pqr","rty'gjg","sdhf,sjh","dhd.jkyt");
I want to replace all the occurrences of hyphen, space, comma, dot, single quotes with underscore.
How can I do a replace with pattern.
I know I have to use
preg_replace($pattern,$replacewith,$string);
But I dont know how to use it.
Hope this simple preg_replace will help you out.
Regex: [\'\.,\s-]
1. [\'\.,\s-] this will match either of these characters , ,-,., ' or space
Try this code snippet here
<?php
ini_set('display_errors', 1);
$arr = array("abc-xyz","a pqr","rty'gjg","sdhf,sjh","dhd.jkyt");
$arr=preg_replace('/[\'\.,\s-]/', "_", $arr);
print_r($arr);
I'm working with text content in UTF8 encoding stored in variable $title.
Using preg_replace, how do I append an extra space if the $title string is ending with:
upper/lower case character
digit
symbol, eg. ? or !
This should do the trick:
preg_replace('/^(.*[\w?!])$/', "$1 ", $string);
In essence what it does is if the string ends in one of your unwanted characters it appends a single space.
If the string doesn't match the pattern, then preg_replace() returns the original string - so you're still good.
If you need to expand your list of unwanted endings you can just add them into the character block [\w?!]
Using a positive lookbehind before the end of the line.
And replace with a space.
$title = preg_replace('/(?<=[A-Za-z0-9?!])$/',' ', $title);
Try it here
You may want to try this Pattern Matching below to see if that does it for you.
<?php
// THE REGEX BELOW MATCHES THE ENDING LOWER & UPPER-CASED CHARACTERS, DIGITS
// AND SYMBOLS LIKE "?" AND "!" AND EVEN A DOT "."
// HOWEVER YOU CAN IMPROVISE ON YOUR OWN
$rxPattern = "#([\!\?a-zA-Z0-9\.])$#";
$title = "What is your name?";
var_dump($title);
// AND HERE, YOU APPEND A SINGLE SPACE AFTER THE MATCHED STRING
$title = preg_replace($rxPattern, "$1 ", $title);
var_dump($title);
// THE FIRST var_dump($title) PRODUCES:
// 'What is your name?' (length=18)
// AND THE SECOND var_dump($title) PRODUCES
// 'What is your name? ' (length=19) <== NOTICE THE LENGTH FROM ADDED SPACE.
You may test it out HERE.
Cheers...
You need
$title=preg_replace("/.*[\w?!]$/", "\\0 ", $title);
I tried to search around but couldn't find anything useful. I need to trim special characters from beginning and end of a string and identify if the remaining portion is a number.
For example
(5)
[[12]]
{3}
#!8(#
!255=
/879/
I need a preg_match expression for it. The regular expression should ignore the string if any alphabets come in between.
$string="yourstring";
$new_string=preg_replace('/[^A-Za-z0-9]/', '', $string);
if(is_numeric($new_string){
echo "number";
} else {
echo "string";
}
^(?!.*[a-zA-Z])\W*(\d+)\W*$
You can use this.Lookahead will validate if only numbers are there.Replace by $1.See demo.
https://regex101.com/r/cT0hV4/2
I need to remove all square brackets from a string and keep the string. I've been looking around but all topic OP's want to replace the string with something.
So: [[link_to_page]]
should become: link_to_page
I think I should use php regex, can someone assist me?
Thanks in advance
You can simply use a str_replace.
$string = str_replace(array('[[',']]'),'',$string);
But this would get a '[[' without a ']]' closure. And a ']]' without a '[[' opening.
It's not entirely clear what you want - but...
If you simply want to "remove all square brackets" without worrying about pairing/etc then a simple str_replace will do it:
str_replace( array('[',']') , '' , $string )
That is not (and doesn't need to be) a regex.
If you want to unwrap paired double brackets, with unknown contents, then a regex replace is what you want, which uses preg_replace instead.
Since [ and ] are metacharacters in regex, they need to be escaped with a backslash.
To match all instances of double-bracketed text, you can use the pattern \[\[\w+\[\] and to replace those brackets you can put the contents into a capture group (by surrounding with parentheses) and replace all instances like so:
$output = preg_replace( '/\[\[(\w+)\[\]/' , '$1' , $string );
The \w matches any alphanumeric or underscore - if you want to allow more/less characters it can be updated, e.g. \[\[([a-z\-_]+)\[\] or whatever makes sense.
If you want to act on the contents of the square brackets, see the answer by fluminis.
You can use preg_replace:
$repl = preg_replace('/(\[|\]){2}/', '', '[[link_to_page]]');
OR using str_replace:
$repl = str_replace(array('[[', ']]'), '', '[[link_to_page]]');
If you want only one match :
preg_match('/\[\[([^\]]+)\]\]/', $yourText, $matches);
echo $matches[1]; // will echo link_to_page
Or if you want to extract all the link from a text
preg_match_all('/\[\[([^\]]+)\]\]/', $yourText, $matches);
foreach($matches as $link) {
echo $link[1];
}
How to read '/\[\[([^\]]+)\]\]/'
/ start the regex
\[\[ two [ characters but need to escape them because [ is a meta caracter
([^\]]+) get all chars that are not a ]
\]\] two ] characters but need to escape them because ] is a meta caracter
/ end the regex
Try
preg_replace(/(\[\[)|(\]\])/, '', $string);
I have string like below,
$string = "test coontevt [gallery include=\"12,24\"] first [gallery include=\"12,24\"] second";
i need to remove the string starts with [gallery to first ocuurance of it's ].
i already use this one,
$string12 = preg_replace('/[gallery.+?)+(/])/i', '', $string);
but i get empty string only.
Finally i want result for the above string is,
$string ="test coontevt first second".
How can i do this using regular expression?.
plz help me?
The character [ is a regex meta-character. TO match a literal [ you need to escape it.
$string12 = preg_replace('/\[gallery.+?\]/i', '', $string);
or
$string12 = preg_replace('/\[gallery[^\]]+\]/i', '', $string);
You need to escape the square brackets
$string12 = preg_replace('/\[gallery.+?\]/i', '', $string);
The round brackets are unnecessary so I removed them, also the quantifier between those brackets and the forward slash before the last square bracket.
To avoid multiple space in the result, I would match also the surrounding spaces and replace with 1 space.
\s+\[gallery.+?\]\s+ and replace with one space
$string12 = preg_replace('/\s+\[gallery.+?\]\s+/i', ' ', $string);
See this expression here online on Regexr
Try it like this:
$string12 = preg_replace('/\[gallery[^\]]+\]/i', '', $string);
[^\]]+ means that there can be one or more character that is not ]. And there is no need for any ( and ) if you don't want to use the backreferences.