Palindrome Check

Need to check words from five characters that are Palindrome . That is, read equally both on the left and right.

Tell me where to repel to perform the task, literature or code examples.


Answer 1, Authority 100%

function isPalindrome($w){
    for($i = 0, $l = strlen($w)-1, $il = ceil($l/2); $i < $il; ++$i)
        if($w[$i] != $w[$l-$i])
            return false;
    return true;
}
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
preg_match_all('/\b(\w{5})\b/i', $text, $m);
for($i = 0, $il = sizeof($m[1]); $i < $il; ++$i)
    if(isPalindrome($m[1][$i]))
        echo $m[1][$i], '<br />'; //      ,    

Answer 2, Authority 50%

Do not complicate anything.

1) in the verifiable word – 5 letters, if the length is different from 5 – return false;

2) if the first character of the word is equal to the fifth and second equal to the fourth – return TRUE;

3) In other cases, we return false.

If you wish, you can enjoy the letters to a single register, but there is no such task condition.

  function isPal($w)
    {
        if (strlen($w) <> 5) return 0;
        if (($w[0] == $w[4]) && ($w[1] == $w[3])) return 1; 
        return 0;
    }

Answer 3, Authority 25%

class palindrome{
    //  
    public function getMorePalindrome($str){
        $sentence = mb_strtolower($str);
        $sentence = str_replace(' ', '', $sentence);
        return '  "'.$str.'" ('.$sentence.'::'.$this->strrev_enc($sentence).')  "'.$this->getPalindrome($str).'" <hr>';
    }
    //  
    public  function getPalindrome($str) {  
        $sentence = mb_strtolower($str);
        $sentence = str_replace(' ', '', $sentence);
        //   
        if(!mb_strlen($sentence)){
            return '';
        }
        $revSentence = $this->strrev_enc($sentence);
        //   
        if($sentence == $revSentence){
            return $str;
        }
        $arrP = $this->searchAllP($sentence, $revSentence);
        for($i=0, $palindrome='', $l=0; $i<count($arrP); $i++){
            $strLength = iconv_strlen($arrP[$i], 'UTF-8');
            if($l<$strLength){
                $l = $strLength;
                $palindrome = $arrP[$i];
            }           
        }
        return $palindrome;
    }
    //   
    public  function strrev_enc($str){
        $str = iconv('utf-8', 'windows-1251', $str);
        $str = strrev($str);
        $str = iconv('windows-1251', 'utf-8', $str);
        return $str;
    }
    //   
    protected function searchAllP($str, $rev){
        $arrP = array();
        $words = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);  
        for($i=0; $i<count($words); $i++){
            $reg = preg_quote($words[$i]);
            $arrP[] = $this->searchLengthP($reg, $rev, $i, $words);                             
        }
        return $arrP;       
    }
    //   
    protected function searchLengthP(&$reg, &$rev, $i, &$words, $sw = ''){
        if(preg_match('#'.$reg.'#u', $rev, $m)){
            $sw = $m[0];
            if($i+1<count($words)){
                $i++;
                $reg .= preg_quote($words[$i]);
                return $this->searchLengthP($reg, $rev, $i, $words, $sw);
            }
        }
        return $sw;
    }
}
$palindrome = new palindrome();
echo $palindrome->getMorePalindrome("lol   lol");
echo $palindrome->getMorePalindrome("+");
echo $palindrome->getMorePalindrome("  ");
echo $palindrome->getMorePalindrome("  ");
echo $palindrome->getMorePalindrome("Sum summus mus");

From the string “Alol Leps Sleep LOL” (alolleptlolol :: Lolleptlololol) turned out to be “LOLLEPSAPLOLOLOL”


From the line “Leps + Sleep” (Leps + Svew :: Aleps + Svew) turned out “Leps + Svew “
From the line” My Leps Spere “(washed :: Alepponel) turned out to” Lepsaspel “
from the line” Argentina Manit Negro “(Argentinamanithemtem :: Argentinamanitheneg) turned out to be” Argentina Manit Negro “
from the string” SUM ” Summus Mus “(Sumsummusmus :: Sumsummusmus) turned out to” Sum Summus Mus “


Answer 4, Authority 25%

Another option (ang + rus) …

$l ='';
$a = ' ';
$s = str_replace(' ', '', $a);
$s = mb_strtolower($s);    
$k = 2;
    if(preg_match("([a-z])", $s))$k = 1;
$c = str_split($s, $k);
$g = implode("", array_reverse($c));
    if ($s != $g) $l = ' '; 
    echo "$a $l   <br>";

Answer 5

$str='abccba';
$strrev=strrev($str);
if ($str==$strrev) {echo ' !!!!';}
else {echo ' <b></b>  !!!';}

Answer 6

Why complicate everything? 🙂

<?
$string = "asdsa";
for ($i=strlen($string); $i >= 0; $i--)
    {
        $str .= $string[$i];
    }
if($str == $string)
    echo "True";
else
    echo "False";

? & gt;

We read the word from the end. Next, write to the $ STR variable each symbol.
If $ STR == $ String means everything is working :)))

either can be more trial :))) (if necessary, for example, to display a list of words from the line that will be submitted)

$string = "asdsa1 jfsdd asdsa qwewq 12345 nghty mghtr trert";
$string = explode(" ", $string);
for($i=0; $i< count($string); $i++){
    for ($b=strlen($string[$i]); $b >= 0; $b--)
        {
            $str[$i] .= $string[$i][$b];
        }
    if($str[$i] == $string[$i])
        print_r($str[$i]."\r\n");
}

Sound – the algorithm has changed slightly.
Line experiments on spaces, we obtain an array of words. And in the old manner (as described above) 🙂


Answer 7

I offer another search option for palindromes with a form in Russian and English:

<?php
if (isset($_REQUEST['w'])) { 
    $p = $_REQUEST['w'];
    $r="";
    $p = mb_strtolower($p);
    $p = str_replace(' ', '', $p);
    $c = mb_strlen($p, 'utf-8');
    for ($i=1; $i<=$c; $i++) {
        $r .= mb_substr($p, -$i, 1, 'utf-8');
    }
    if ($p == $r){
        echo "$p   <br>";
    } else {
        echo "$p    <br>"; 
    }
}
?>
<form action="" method="post">
   : <input type="text" name="w" value=""><br />
  <input type="submit" name="doSub" value="">
</form> 

Answer 8

I had a similar task, but it was impossible to use cycles.
I am a beginner, so most likely the code is not optimal, but it seems to work.

function isPalindrome($text) {
    static $i = 0;
    $length = strlen($text);
    $middle = ceil($length/2);
    $text_1 = str_split($text);
    if ($i < $middle) {
    if ($text_1[$i] == $text_1[$length-$i-1]) {
        ++$i;
        isPalindrome($text);
    }
}
if ($i == $middle) return true;
}