find exact word in a var string in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i'm trying to find an exact word inside a string.
example:
$word = "Many Blocks";
if (strpos($word, "Block")){
echo "You found 1 Block";
}
if (strpos($word, "Blocks")){
echo "You found many Blocks";
}
The problem here is both if are true.. and i need to find only if is the same word..

As Jay Blanchard says you need to do it with regex in following way:--
$word = "Many Blocks";
if ( preg_match("~\bBlocks\b~",$word) )
echo "matched";
else
echo "no match";

Your code would work with an offset on the second search and a few other changes.
$result = 'You found no blocks';
$position = strpos($word, "Block");
if ($position !== false){
$result = "You found 1 Block";
if (strpos($word, "Blocks",$position + 1)){
$result = "You found many Blocks";
}
}
echo $result;
by using the strpos() offset you can keep looping through until the word is no longer found.
$found = 0;
$offset = 0;
while(true){
$position = strpos($word,'Block',$offset );
if ($position === false){break;}
$found++;
$offset = $position + 1; // set offset just beyond the current found word
}
echo "Found: $found";
or
This one the code is simple but is slower:
preg_match_all('/Blocks/',$word,$matches);
$found = count($matches[1]);

You can do it with regex like this:
if(preg_match("/Block(\s|$|\.|\,)/", $string))
This looks for the word "Block" followed by space or dot or comma or end of string.

Related

Obfuscate email address on php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to obfuscate or cover an email address on PHP.
For this, i have the next code.
<td>
<p class="list-item-heading"><small><?= $row->email ?></small></p>
</td>
Where $row->email is
realname_x123#gmail.com
.
I need to show this as
r*al****_x*2*#gmail.com
I need to replace with * ALWAYS the same parts of the string, not randomly because it will be shown on a list, maximun of 5 or 6 characters visible.
Anytips? I've tried with strpos, and str_replace with no success.
EDIT:
IF this cannot be do. It will be usefull also, for example, to only leave 3 chars from the beggining.
rea***********#gmail.com
I've found a workaround that suits for me.
<?php
function hideEmail($email)
{
$mail_segments = explode("#", $email);
$mail_segments[0] = substr($mail_segments[0], 0, 1) . str_repeat("*", strlen($mail_segments[0]) - 2) . substr($mail_segments[0], -1);
$pos = strpos($mail_segments[1], '.');
$mail_segments[1] = substr($mail_segments[1], 0, 1) . str_repeat("*", strlen($mail_segments[1]) - $pos+1) . substr($mail_segments[1], $pos-1);
return implode("#", $mail_segments);
}
?>
function mailObfuscate($mail) {
//every second character replace with *
$mail = explode('#',$mail);
$array = str_split($mail[0]);
$control = 0;
$ret = '';
foreach ($array as $char) {
if ($control == 0) {$ret .= '*'; $control++;} else {$ret .= $char; $control=0;}
}
return $ret.'#'.$mail[1];
}

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

Parse url and choose specific number with php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How is it possible to choose a specific word/s or number/s from a link with php. I have the following urls where I want to choose only the rss number. Have been trying to use preg_match and preg_replace to no avail unfortunately.
<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>
<link><![CDATA[http://www.domain.com/rugby/video/ball/index.html?rss=2133483+water+none+respective+all+sat's+report]]></link>
As you can see the urls are not the same but both have rss=XXXXXX. My aim is to insert the number after "rss=" into the database.
Would appreciate if anyone can give me a tip of how to do this.
Like this ?
<?php
$link = '<link><![CDATA[http://www.domain.com/league/news/newsid=21898248704.html?rss=2148704+hakska+iwumao+oioqp+badge+water]]></link>';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
?>
$url = "http://www.example.com/foo.php?rss=9001+asdf";
$res = "";
$i = 0;
if (($i = strpos($url, 'rss=')) !== false) {
$res = substr($url, $i + 4); // +4 because the length of 'rss=' is 4, we just want the value
if (($i = strpos($res, '+')) !== false) { // $res contains +, cut that off
$res = substr($res, 0, $i);
}
// res has a value
echo $res;
} else {
// res has no value
}
Example
$link = '';
preg_match('/rss=([0-9]+)/', $link, $matches);
echo $matches[1]; // returns 2148704
Worked fine. Thanks a lot Phantom!

PHP Uppercase first three words of a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I uppercase the first three words of a string?
I've tried using the below, but it doesn't work.
<p><?php echo strtoupper(substr($copy, 0, 3)) . substr($copy, 3); ?></p>
Thanks
This iterates over the first three words found in the string and applies uppercase:
$copy = 'hello world bla hohoho';
echo preg_replace_callback('/\w+/', function($m) {
return strtoupper($m[0]);
}, $copy, 3);
$words = preg_split('/\s/', $copy);
if(is_array($words)) {
$counter = 0;
foreach($words as $word) {
if($counter < 3) {
echo ucfirst(strtolower($word)), " ";
} else {
echo $word, " ";
}
$counter++;
}
}
Example

Define array with variable index [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My code:
$rank_content = file('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2');
$line_count = 0;
//initializing only the first few keys because of no reason (the latter ones aren't in use yet)
$rankNameArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
$rankRlmpArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
while ($line = array_shift($rank_content)) { // retrieving line after line of the website, does work indeed
$line_count += 1;
if(strpos($line, "Warrior #") || strpos($line, "Archer #") || strpos($line, "Mage #"))
{
$rankNameArr[$line_count] = $line_count + 1; // HERE nothing happens
$rankRlmpArr[$line_count] = $line_count + 2; // nothing happens here, too
}
}
Why does
echo $rankNameArr[2];
echo $rankRlmpArr[2];
give me the value 42 instead of the correct value? If I replace $line_count with a real number, the script works properly.
My intention is to store the value $line_count + 1 into $rankNameArr at position $line_count. Not very complicated actually
EDIT -------------
Forget everything above, please. I finally reduced the script to the actual problem:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$arr[$counter]=$i;
}
echo $arr[5];
This sadly echoes 42. I have no idea how to have $arr[$counter] store the actual value of $i.
I have no idea how to have $arr[$counter] store the actual value of $i.
$counter is set to 0, so $arr[$counter] is the same as saying $arr[0]. If you echo $arr[0] you'll see that it is being changed while the rest are left alone, which means your code is working.
However if you want $counter to increment also, you just need to tell it to do so:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$counter++;
$arr[$counter]=$i;
}
echo $arr[5];

Categories