php


i want to compare the string and show to characters which match in both strings in php


i want to compare the string and show to characters which match in both strings in php i tried everything but failed to do this please give me any idea how to do this in php
e.g i have two variables
I have to compare $a and $b and give the output as letters whicj are common in both
$a="hello ";
$b= "hell";
output should be : :: hell as first 4 character matches so it should show hell please help me to do this
i have tried everything almost everything which i know or could i find on web
What I tried.. I spit the strings to array... Using nested for loop to find the non matched letters... I wrote code more than 35 lines.. But no result :( Please help me ......
In your case, it would be enough to use array_intersect and str_split functions to get characters common to both input strings(of course, if order of characters doesn't matter):
$a = "hello ";
$b = "hell";
$common = array_intersect(str_split(trim($a)), str_split(trim($b)));
print_r($common);
The output:
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
)
http://php.net/manual/en/function.array-intersect.php
This PHP code is the one you need.
<?php
$a = "hello";
$b = "hell";
$str = "";
for ($i=0; $i < strlen($a); $i++) {
for ($j=0; $j < strlen($b); $j++) {
if ($a[$i]==$b[$j]) {
$str.=$a[$i];
break;
}
}
}
echo $str;
?>
PHP Strings can be seen as Char arrays so $a[0] get you the first char of the string.

Related Links

Sending boolean values with $.ajax to PHP
Loop row in bootstrap every 3 columns
Get friends name/id list form graph api into array
LAMP shows index.php in var/www/html instead of project
How To play 25MB+ audio file directly from google drive in HTML 5?
PHP/MYSQL - Select option value not being sent?
Laravel : How to use a parameters in a Form POST to be use in a Route::post?
How to convert object format to json format in php
laravel,how i can change the code to return an array?
How to make At Least Two Field Required to fill in Laravel 5.2
How do I allow PHP to run when the script is not in the URL?
How to check if a file is BEING used, i.e. some command is being executed on the data in that file?
Display profile data from database when login
How to integrate laravel packages into an application
Log into a Django project using Magento user database SSO
Image not show in recyclerview from server

Categories

HOME
jms
android-fragments
d3.js
haproxy
kivy
ant
android-emulator
swift2
mailing-list
mutation-testing
msmq
colors
phpstorm-2017.1
clish
orc
responsivevoice
feature-extraction
freertos
google-awareness
rpmbuild
wsf
jest
yii2-basic-app
weex
gollum-wiki
acl
sipp
fatfs
mapnik
epplus
private-key
osx-lion
om-next
ms-media-foundation
orchardcms-1.8
busboy
code-rally
spring-restcontroller
utf
suds
contactless-smartcard
apache-toree
tinyioc
image-registration
beeline
nusoap
head.js
nslayoutconstraint
mobile-angular-ui
.when
gawk
haraka
thread-exceptions
okio
jsonix
ndk-build
nunit-console
ftp-client
callouts
raw-sockets
meld
accessibilityservice
as3-api
google-maps-api-2
ruby-on-rails-4.1
mixins
canopy
componentart
operands
nidaqmx
boolean-algebra
profiles
vlab
colon
indesign-server
arel
towers-of-hanoi
audiotoolbox
telerik-ajax
thejit
startupscript
jmenu
firephp
powergui
rtti
gtktextview

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App