preg_replace text highlight to ignore URLs - php

i am using preg_replace for highlighting words in search results. search result sometimes also contains URL, not just text. and some URLs contain key words. then URLs get messed up as preg_replace also changes the URL.
is there any way to ignore URLs in preg_replace?
this is what i use:
$result = preg_replace('!('.$keyword.')!i', '<span style="background: #f00;">$1</span>', $result);
thank you!

Edited..
okay, than is this helpful?
Make your result as array and then check if it contains url?
<?php
$result = "This is Stpartāāa http://google.lv ";
$arr = explode(" ", $result);
foreach($arr as $key => $value) {
if ((strpos($value,'http://') !== false) AND (strpos($value,'www.') !== false)) {
// do nothing
} else {
// do somthing
}
}
?>

Related

Get characters right after match in a foreach loop

On my site I want to detect if someone mentions a username in a comment, like so: what's up /u/username.
How exactly can I extract the characters following /u/ in a foreach loop?
Something like this:
if (strpos($commentString, '/u/') !== false) {
foreach /u/ in $commentString {
$username = the text immediately after /u/, stopping at anything that isn't a letter or a number
}
}
You can use preg_match_all with a regex of
/u/([a-z0-9]+)
to capture the usernames in the text. For example:
$text = "what's up /u/username have you seen /u/user21 today?";
preg_match_all('#/u/([a-z0-9]+)#i', $text, $matches);
foreach ($matches[1] as $user) {
echo "found user $user\n";
}
Output:
found user username
found user user21
Demo on 3v4l.org

regex php special characters

$word = file_get_contents('http://www.pixelmon-server-list.com/list.txt');
$content = file_get_contents('http://www.pixelmon-server-list.com/fleetyfleet.txt');
$found_dimensions = array(); // our array
$word_array = explode(' ', $word); // explode the list
foreach($word_array as $one_word) { // loop over it
$str = 'DimensionName'.$one_word; // what are we looking for?
if(strstr($content, $str) !== false) { // look for it!
echo $one_word; // Just for demonstration purposes
$found_dimensions[] = $one_word; // add to the array
}
}
okay i have a list.text and a fleetyfleet.txt
both can be viewed here i didn't post them for space sake
http://pastebin.com/7hWDUG1b
but what i want to do is find the words in list.txt but only add them to array if there prefix is Dimension�����Name� but the special characters make it kinda tough I'm not sure what i should do
I had a similar problem where I needed to remove all non-ascii characters from a file. Here's the regex I used:
s/[^\x00-\x7F]//g
If you're on linux, here's a quick one-liner:
perl -p -i -e "s/[^\x00-\x7F]//g" list.txt
Having to guess a little bit as I can't see the files from behind my firewall. The following code might help you:
<?php
$f1 = explode("\n",file_get_contents("./file1.txt"));
$f2 = file_get_contents("./file2.txt");
$found = array();
foreach($f1 as $x) {
$str = "/DimensionName.....$x./";
if (strlen($x)>0) {
if (preg_match($str, $f2, $matches)) {
echo $matches[0]."\n";
}
}
}
?>
This prints out lines that include a pattern DimensionName followed by 5 "anything" followed by whatever word was read from the first file file1.txt.
If you need this to be further refined, please leave a comment.

Replace text if it matches another piece of text

So I have the following code to remove "page=" from a string. My problem now is that I want to query through "$qs_final" to check if it contains "price_range" and if so replace it with another piece of text. The price range variable is attached to "attr=" so I can't really use a $_GET request as other information is stored within it. The price_range variable also has the layout of "price_range_20".
<?php
$querystring = explode("&",$_SERVER['QUERY_STRING']);
$qs_nos = 0;
$qs_final = "";
while(isset($querystring[$qs_nos])) {
if(!ereg("page=",$querystring[$qs_nos])) {
$qs_final .= $querystring[$qs_nos]."&";
}
$qs_nos++;
}
if (strpos($qs_final,'price_range') !== false) {
print "true";
}
?>
str_replace().
$new_string = str_replace($what_to_replace, $what_to_replace_it_with, $old_string);
EDIT: To replace data, you need preg_replace(). In your case to remove "price_range" and all numbers and underscores directly after it, use this:
$new_string = preg_replace("/price_range[0-9_]+/", "", $old_string);

I cannot get strpos to work with newline

I am very new to php and cannot find out how to fix this. I have a form and want to pass some string to it and have it checked with another file. It is working fine if all the strings are on the same line but as soon as I put in multiple lines of string, it will fail.
I have a php file with the following code:
<?php
echo "<center><form method='post' enctype='multipart/form-data'>";
echo "<b></b><textarea name='texttofind' cols='80' rows='10'></textarea><br>";
echo "<input name='submit' type='submit' style='width:80px' value='Run' />";
echo "</form></center>";
$texttofind = $_POST['texttofind'];
if(get_magic_quotes_gpc()) {
$texttofind = stripslashes($texttofind);
}
$texttofind = html_entity_decode($texttofind);
$s = file_get_contents ("/home/xxxxx/public_html/abc.txt");
if (strpos ($s, $texttofind) === false) {
echo "not found";
}
else
echo "found";
?>
and in abc.txt, I have
dog
cat
rat
Whenever, I open the php page and type in just dog or cat, it will be fine and show 'found' message but when I type multiple lines like 'dog<enter on keyboard>cat' and click submit button, it will return with the 'not found' message.
What is wrong with the code or anyway to adapt it so that it will be able to search multiple lines?
Thank you in advance.
When you place your search words on new lines you are adding characters that do not exist in your comparison file. Eg when you enter...
dog
cat
rat
You are actually sending a string that looks like...
"dog\ncat\nrat"
Where \n means character 13 or the standard non windows new line character. The fix for this depends on what you want to do. You can search for results using PHP's explode function to convert the input string into an array and then get positions for each word...
$inputs = explode("\n", $_POST['field']);
$positions = array();
foreach($inputs as $val)
$positions[] = str_pos($compareTo, $val);
Now $positions should be an array of the str_pos's that where found for each line.
If you are still trying to search that the comparison file has all of the text you just don't care if it is on a new line or not you can simply strip out the new line characters all together (also remove \r just to be safe)
$inputs = str_replace("\n", "", $_POST['field']);
$inputs = str_replace("\r", "", $inputs);
Now inputs would be "dogcatrat". You can use the second argument of str_replace to set a space instead of a \n to get back to the space separated list.
$inputs = str_replace("\n", " ", $_POST['field']);
$inputs = str_replace("\r", "", $inputs);
Yes we are still ignoring \r all together (silly windows). All the same I suggest reading up on how to use array's, explode & implode and str_replace. Many will comment on this and tell you that str_replace is bad and that you should learn regex. As an experienced developer I find very few cases where regex replacements provide any better functionality in simple character replacement and it will cause you to learn a completely new language of commands. For now ignore those who tell you to use regex but definitely learn regex in the near future. You will need it eventually, just not for something of this nature.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
http://php.net/manual/en/function.str-replace.php
http://php.net/manual/en/function.preg-match.php
<?php
$values=explode("\n",$txttofind);
foreach($values as $value)
{
if (strpos ($s, $value) === false)
{
echo "$value : not found <br>";
}
else
{
echo "$value : found <br>";
}
}
?>

determine if a string contains one of a set of words in an array

I need a simple word filter that will kill a script if it detects a filtered word in a string.
say my words are as below
$showstopper = array(badword1, badword2, badword3, badword4);
$yourmouth = "im gonna badword3 you up";
if(something($yourmouth, $showstopper)){
//stop the show
}
You could implode the array of badwords into a regular expression, and see if it matches against the haystack. Or you could simply cycle through the array, and check each word individually.
From the comments:
$re = "/(" . implode("|", $showstopper) . ")/"; // '/(badword1|badword2)/'
if (preg_match($re, $yourmouth) > 0) { die("foulmouth"); }
in_array() is your friend
$yourmouth_array = explode(' ',$yourmouth);
foreach($yourmouth_array as $key=>$w){
if (in_array($w,$showstopper){
// stop the show, like, replace that element with '***'
$yourmouth_array[$key]= '***';
}
}
$yourmouth = implode(' ',$yourmouth_array);
You might want to benchmark this vs the foreach and preg_match approaches.
$showstopper = array('badword1', 'badword2', 'badword3', 'badword4');
$yourmouth = "im gonna badword3 you up";
$check = str_replace($showstopper, '****', $yourmouth, $count);
if($count > 0) {
//stop the show
}
A fast solution involves checking the key as this does not need to iterate over the array. It would require a modification of your bad words list, however.
$showstopper = array('badword1' => 1, 'badword2' => 1, 'badword3' => 1, 'badword4' => 1);
$yourmouth = "im gonna badword3 you up";
// split words on space
$words = explode(' ', $yourmouth);
foreach($words as $word) {
// filter extraneous characters out of the word
$word = preg_replace('/[^A-Za-z0-9]*/', '', $word);
// check for bad word match
if (isset($showstopper[$word])) {
die('game over');
}
}
The preg_replace ensures users don't abuse your filter by typing something like bad_word3. It also ensures the array key check doesn't bomb.
not sure why you would need to do this but heres a way to check and get the bad words that were used
$showstopper = array(badword1, badword2, badword3, badword4);
$yourmouth = "im gonna badword3 you up badword1";
function badWordCheck( $var ) {
global $yourmouth;
if (strpos($yourmouth, $var)) {
return true;
}
}
print_r(array_filter($showstopper, 'badWordCheck'));
array_filter() returns an array of bad words, so if the count() of it is 0 nothign bad was said

Categories