Skip to main content

Get Text difference between new and old

Sample output:
THis are my car.
This is my car.

function htmlDiff($old, $new){
        $diff = self::diffArray(explode(' ', $old), explode(' ', $new));
        $flag = 0;
        $ret = "";
        foreach($diff as $i => $k){
            if(is_array($k)){
                if((count($k['d']) != 0) && (count($k['i']) == 0)){
                    $flag = 1;
                }
                else if((count($k['d']) != 0) && (count($k['i']) != 0)){
                    $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
                }
                else if((count($k['d']) == 0) && (count($k['i']) != 0)){
                    $ret .= (!empty($k['i'])?"<span style='background-color:#ccffcc'>".implode(' ',$k['i'])."</span> ":'');
                }
            }
            else{
                if($flag == 0){
                    $ret .= $k . ' ';
                }
                else if($flag == 1){
                    $pre = $i - 2;
                    $post = $i;
                    $ret= preg_replace('/\W\w+\s*(\W*)$/', '$1', $ret);
                    $ret .= " <span style='background-color:#ccffcc'>".$diff[$pre]." ".$diff[$post]."</span> ";
                }
                $flag = 0;
            }
        }
        return $ret;
    }

    public function diffArray($old, $new){
        $matrix = array();
        $maxlen = 0;
        foreach($old as $oindex => $ovalue){
            $nkeys = array_keys($new, $ovalue);
            foreach($nkeys as $nindex){
                $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
                if($matrix[$oindex][$nindex] > $maxlen){
                    $maxlen = $matrix[$oindex][$nindex];
                    $omax = $oindex + 1 - $maxlen;
                    $nmax = $nindex + 1 - $maxlen;
                }
            }
        }
        if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
        return array_merge(
            self::diffArray(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
            array_slice($new, $nmax, $maxlen),
            self::diffArray(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
    }

Comments

Popular posts from this blog

Issue - Laravel Socialite: Legacy People API has not been used in project

If you have an issue with Google Login in Laravel Socialite, Follow these instructions. Change this file ./vendor/laravel/socialite/src/Two/GoogleProvider.php with this: <?php namespace Laravel\Socialite\Two; use Illuminate\Support\Arr; class GoogleProvider extends AbstractProvider implements ProviderInterface {     protected $scopeSeparator = ' ';     /**      * The scopes being requested.      *      * @var array      */     protected $scopes = [         'openid',         'profile',         'email',     ];     /**      * {@inheritdoc}      */     protected function getAuthUrl($state)     {         return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state); ...

get records using location by dynamic radius from database.

$postads = PostAds::with('userInfo')                             ->has('userInfo')                             ->where('user_id','!=',Auth::id())                             ->whereRaw('(ST_Distance_Sphere(POINT('.$lng.','.$lat.'), POINT(longitude,latitude))/1000) <= radius')                             ->paginate(10);

Paypal Payment API

/*******************/ $payment_type = 'Sale'; $amount = $invoice->price + $invoice->due_price + $invoice->service_charge; $send = 'VERSION=51.0'; $send.= '&USER=' . urlencode('Credit card Username'); $send.= '&PWD=' . urlencode('Credit Card Password'); $send.= '&SIGNATURE=' . urlencode('Credit Card Signature'); $send.= '&METHOD=DoDirectPayment'; $send.= '&PAYMENTACTION=' . $payment_type; $send.= '&IPADDRESS=' . urlencode($_SERVER['REMOTE_ADDR']); $send.= '&AMT=' . $amount; $send.= '&CREDITCARDTYPE=' . $request->cc_type; $send.= '&ACCT=' . urlencode(str_replace(' ', '', $request->cc_number)); $send.= '&EXPDATE=' . urlencode($request->cc_expire_date_month . $request->cc_expire_date_year); $send.= '&CVV2=' . urlencode($request->cvv); $send...