My current code is:
$epattern[17] = "/#(\\w+)/";
$ereplace[17] = "<a href=viewprofile.php?username=$1><font color=royalblue><b>#\\1</b></font></a>";
$postinforawb = preg_replace($epattern,$ereplace,$postinfo);
With the above code the text will highlight blue where the # symbol was used up to where a space has been entered. However I now also want it to include the "+" symbol in posts. So that the following would highlight blue: "#First+Second"
What am I needing to add to the replace?
This will do in your case:
$epattern[17] = "/#([\w\+]+)/";
But i prefer this one as you are only allowing alphabet and +:
$epattern[17] = "/#([a-zA-Z\+]+)/";
$epattern[17] = "/#([\w\+]+)/";
Related
I have a an url which looks like this https://URL.DOMAIN/blog.php?id=43&q=echo%20%27test%27.
When I use <?php echo $_GET['q'] ?> it displays echo 'test' which is what I want.
I am using this variable inside a preg_replace function which is basically made to apply a yellow background under matched strings:
preg_replace('/\b('.$_GET['q'].')\b/iu', '<span class="research-news-found">$1</span>', $news_content);
It works perfectly for "normal" strings like "apple" or whatever, but when there is a ' inside the search query it doesn't match anything.
Code example
$news_content = $news_display['news_description'];
if(isset($_GET['q'])){
$news_content = preg_replace('/\b('.$_GET['q'].')\b/iu', '<span class="research-news-found">$1</span>', $news_content);
}
$news_display['news_description'] contains the text output from DB.
Just make the pattern greedy ? and remove the trailing word boundary \b since ' is not a word character and is a word boundary:
$news_content = preg_replace('/\b('.$_GET['q'].'?)/iu',
'<span class="research-news-found">$1</span>',
$news_content);
Demo
But if you are hoping that it will actually echo test, then no. You would need to restructure your question to state what you want to achieve, not how to get this replacement to work.
I'm trying to highlight separate characters (letters), in a string using 4 different colors. What I would like is...
Key:
,Y,2 = Red background/white text
,Y,1 = Red background/white text
,T, = Blue background/white text
,N\r\n = Green background/white text
word.it = White background/grey text
The only way to correctly identify only the letters I want to highlight is by their position to other adjacent characters,e.g. commas, numbers and line returns, however I don't actually want to highlight the adjacent delimiters.
*See my HTML mockup of what I'm after here:
I've already tried using preg_replace but the code only allows me to highlight one character in the array in the color I want and not the others too. All letters get assigned just one color using this code.
$sentence = $resp;
$wordsToHighlight = array(",N\r\n", ",T,", "Y,1", "Y,2");
$modifiedData = preg_replace('/'.implode('|', $wordsToHighlight).'/', '<span style="background:#24aaff;"><b>$0</b></span><span style="background:#33cc33;"><b>$1</b></span><span style="background:#ff0000;"><b>$2</b></span><span style="background:ff0000;"><b>$3</b></span>', $sentence);
echo '<p class="names">' .$modifiedData. '</p>';
Please take a look at the following situation below.
[reply="292"] Text Here [/reply]
What I am trying to get is the number between the quotations in reply="NUMBERS". I want to extract that to one variable and the text between [reply="NUMBER"] this text here [/reply] to another variable.
So for this example:
[reply="292"] Text Here [/reply]
I want to extract the reply number: 292 and the text between the reply tags: Text here.
I have tried this:
\[reply\=\"]([A-Z]\w)\[\/reply]
But this only works until the reply tag, doesn't work after that. How can I go about doing this?
I left generic (. *), but you can specify a type like decimal (\d+).
php:
$s = '[reply="292"] Text Here [/reply]';
$expr = '/\[reply=\"(.*)\"\](.*)\[\/reply\]/';
if(preg_match($expr,$s,$r)){
var_dump($r);
}
javascript:
s = '[reply="292"] Text Here [/reply]'
s.match(/\[reply=\"(.*)\"\](.*)\[\/reply\]/)
//["[reply="292"] Text Here [/reply]", "292", " Text Here "]
Easy!
\[reply\=\"(\d+)\"](.*?)\[\/reply]
Explanation
\d for digit
+ for 1 or more occurrence of the specified character.
[\w\s] for any character in word and whitespace (\s)
Then apply it to PHP like this:
<?php
$str = "[reply=\"292\"] Text Here [/reply]";
preg_match('/\[reply\=\"(\d+)\"]([\w\s]+)\[\/reply]/', $str, $re);
print_r($re[1]); // printing group 1, the reply number
print_r($re[2]); // printing group 2, the text
?>
Important!!
Just get the group value, not all. You only need some of it anyway.
I would like to change all emoticons codes from my string to emoticons images.
Here is my array with codes and URLs:
$smilies = array(
array('http://page.com/facebook-smiley-face-for-comments.png', ':)'),
array('http://page.com/big-smile-emoticon-for-facebook.png', ':D'),
array('http://page.com/facebook-frown-emoticon.png', ':('),
...
);
and here is script which change this codes for images:
foreach($smilies as $emoticon)
{
$quoted_emoticon = preg_quote($emoticon[1],"#");
$match = '#(?!<\w)(' . $quoted_emoticon .')(?!\w)#';
$message = preg_replace($match,'<img src="'.$emoticon[0].'">',$message);
}
But I have one problem with that. I would like to change code for image only when emoticon code have space - before emoticon code and after emoticon code - example:
Here is text :--) with emoticon.
I would like to change also code for image when emoticon is at the end of string. Then emoticon code should have added only space before code:
Here is text with emoticon :--)
And the same situation with the begininnig of a string - emoticon code should have added only space after code:
:--) here is text with emoticon.
Anyone can help me to create these regex ?
Thanks!
Change your regex pattern to allow matches only if a certain emoticon code is not preceded or followed by non-space character:
...
$match = '#(?<!\S)(' . $quoted_emoticon .')(?!\S)#iu';
...
I'm using the code below for highlight one word from file_get_content and go to anchor.
$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';
$Seq = 0002110000001;
$text = preg_replace("/\b($Seq)\b/i", '<span class="highlight"><a name="here">\1</a></span>', $file);
for now this highlight : 0002110000001
i would like to highlight all part of the same index number.
ex:
looking for 0002110000001
highlight this part of txt only where number is 7
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
Any help will be appreciated.
EDIT:
i try to be more specific.
file contain lot of code parts always start by TYx (x is auto numbering)
i have the SEQ number for my search , in ex 0002110000001
the preg_replace("/\b($Seq)\b/i", '\1 find 0002110000001 and higlight them.
what i need is higlight what is between TY7 and TY8 instead of only 0002110000001.
Hope this is clear enough due to my bad english
thanks
You can make use of stripos() and explode() in PHP
<?php
$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';
//$Seq = "0002110000001";
$Seq = "7";
$new_arr=explode(PHP_EOL,$file);
foreach($new_arr as $k=>$v)
{
if(stripos($v,$Seq)!==false)
{
echo "$v\n";
}
}
OUTPUT :
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005