How to check if there is a tab at the end of a string? Which pattern works? I tried /\+t$/ but i get "There is NO tab at the end of the string" even though there is a tabulator at the end.
<?php
$text = "Come to the dark side. We have cookies ";
$pattern = "/\+t$/";
if(preg_match($pattern , $text))
{
echo "There is a tab at the end of the string";
}
else
{
echo "There is NO tab at the end of the string";
}
?>
Your regex /\+t$/ matches a + sign followed by character t at the end of a string.
I guess you want :
/\t$/ : a tabulation at the end of the string
or /\t+$/ : one or more tabulations at the end
Related
I'm using this code inside PHP
case preg_match('/\/start( .*)?/', $text):
echo "got you";
break;
Using this regex all I need to do is catching following structure:
$text needs to be:
/start
or
/start xyz
Where "xyz" stands for random content. These are the two only formats which should be accepted by the regex. For some reason my regex seems to be not working as expected.
This should do the trick:
^\/start\s?[\S]*$
Here is an example in python DEMO:
import re
textlist = ["^/start xyz","/start","/start not to match"]
regex = "^/start\s?[\S]*$"
for text in textlist:
thematch = re.search(regex, text)
if thematch:
print ("match found")
else:
print ("no match sir!")
What it's doing: the line starts with /start and might have space, then there might be any amount of non space (including none) and then the line ends.
Hopefully that helps!
EDIT;
PHP version of this code.
$textlist = array("^/start xyz","/start","/start not to match");
$regex = "#^/start\s?[\S]*$#";
foreach($textlist as $text){
preg_match($regex, $text, $thematch);
if ($thematch){
print ("match found\n");
}else{
print ("no match sir!\n");
}
}
Demo here: https://3v4l.org/OFpnG
How can I remove special characters like ;lt ;gt but not Anchor tag
e.g
<a href="http://www.imdb.com/name/nm0005069/">Spike Jonze</a> This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
should be
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
Here's a quick one for you:
<?php
// SET OUR DEFAULT STRING
$string = '<a href="http://w...content-available-to-author-only...b.com/name/nm0005069/">Spike Jonze</a> This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~<.*?>~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
All I'm doing here is looking for <, followed by any character ., any number of times *, until it matches the next part of the string ?, which is >.
Any time it finds that, it replaces it with nothing. So you're left with the text you want.
Here is a working demo:
http://ideone.com/uSnY0b
use html_entity_decode:
<?php $url = html_entity_decode('<a href="http://www.imdb.com/name/nm0005069/">Spike Jonze</a>');
echo $url;
?>
the output will be:
Spike Jonze
EDIT:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>
I have several text files, included in my code programatically. I need a regular expression, to be able to catch and remove the content of the comments from the catched string. (The comment contains )
The code where I catch the content of the file is:
ob_start();
include($file);
$c = ob_get_contents();
ob_end_clean();
An example content of the $file is as follows:
//Comment here
//Second line
//All the comment lines are optional
<p>Html content here</p>
<?php echo "Php content may also exists!"; ?>
Any help is welcome.
One way of solving this - if the included files are not large - is to use a regex with a multiline flag/modifier that will match all lines starting with //.
See PHP demo:
$re = '~^\s*//.*$\s*~m';
$str = "//Comment here\n//Second line\n//All the comment lines are optional\n<p>Html content here</p>\n<?php echo \"Php content may also exists!\"; ?>";
echo preg_replace($re, "", $str);
The regex breakdown:
^ - start of a line (as we are using a /m modifier)
\s* - optional whitespace (0 or more occurrences)
// - two forward slashes
.* - any zero or more characters other than a newline
$ - end of line (as we are using a /m modifier)
\s* - optional whitespace (to trim the linebreaks).
If you want to remove only the comments until the actual code begins you need a state machine (the regex solution is more compact but removes the line comments anywhere):
$c = "//Comment here\n//Second line\n//All the comment lines are optional\n<p>Html content here</p>\n<?php echo \"Php content may also exists!\"; ?>";
$output = "";
$stripComments = true;
foreach(preg_split("/((\r?\n)|(\r\n?))/", $c) as $line) {
if ( $stripComments ) {
if ( preg_match("~^\s*//~",$line) )
continue;
else {
$stripComments = false;
$output .= $line."\n";
}
}
else
$output .= $line."\n";
}
echo "$output";
I’m trying this, to get word in word boundaries with unicode characters:
if(preg_match("/(?<!\p{L})jaunā(?!\p{L}) Iel.*/iu", "Jaunā. Iela") > 0){
echo "<h1>Match!</h1>";
}
else{
echo "<h1>dont match</h1>";
}
Why I’m getting „Don’t match”?
I want to find word “jaunā” in all variations for example:
text ,jaunā text
text jaunā, text
text ,jaunā! text
jaunā text
jaunā, text
etc.
Thanks.
You can try with following regex:
/(?<!\p{L})jaunā(?!\p{L}).*? Iel.*/iu
After jaunā you have also . character, so with .*? you can match any other characters between jaunā and Iela
You regex isn't matching a dot after jaunā:
Try this:
if(preg_match("/(?<!\p{L})jaunā(?!\p{L})\. Iel.*/iu", "Jaunā. Iela") > 0) {
echo "<h1>Match!</h1>";
}
else{
echo "<h1>dont match</h1>";
}
I'm trying to append <br/> to all lines that do not end with an html tag, but I'm unable to get it working.
I've got this so far, but it seems to match nothing at all (in PHP).
$message=preg_replace("/^(.*[^>])([\n\r])$/","\${1}<br/>\${2}",$message);
Any ideas on how to get this working properly?
I think you need the m modifier on your regex:
$message=preg_replace("/^(.*[^>])$/m", "$1<br/>\n", $message);
// ^
// Here
m makes ^ and $ match start/end of lines in addition to start/end of string.
No [\n\r] needed.
Also, why do you want to match all the line to just put it back after ?
It is actually as simple as
$message = preg_replace ('/([^>])$/m', '$1<br />', $message);
Example code:
<?php
$message = "<strong>Hey</strong>
you,
No you don't have to go !";
$output = preg_replace ('/([^>])$/m', '$1<br />', $message);
echo '<pre>' . htmlentities($output) . '</pre>';
?>
You can use this:
$message = preg_replace('~(?<![\h>])\h*\R~', '<br/>', $message);
where:
`\h` is for horizontal white spaces (space and tab)
`\R` is for newline
(?<!..) is a negative lookbehind (not preceded by ..)
I've found this that works somehow, see http://phpfiddle.org/main/code/259-vvp:
<?php
//
$message0 = "You are OK.
<p>You are good,</p>
You are the universe.
<strong>Go to school</strong>
This is the end.
";
//
if(preg_match("/^WIN/i", PHP_OS))
{
$message = preg_replace('#(?<!\w>)[\r]$#m', '<br />', $message0);
}
else
{
$message = preg_replace('#(?<!\w>)$#m', '<br />', $message0);
}
echo "<textarea style=\"width: 700px; height: 90px;\">";
echo($message);
echo "</textarea>";
//
?>
Gives:
You are OK.<br />
<p>You are good,</p>
You are the universe.<br />
<strong>Go to school</strong>
This is the end.<br /><br />
Add a < br /> if not ended up with a HTML tag like:
< /p>, < /strong>, ...
Explanation:
(?<!\w>): negative lookbehind, if a newline character is not preceded
by a partial html close tag, \w word character + closing >, like a>, 1> for h1>, ...
[\r\n]*$: end by any newline character or not.
m: modifier for multiline mode.