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

How to write a foreach that will produce the results I want
Where is extension being set?
Call a method from an included file NOT from file that inclued it php
Looping a multidimensional array in php
PHP: how to open a zip
POSTing to different action with Zend_Form (and validation)
PHP GD - Crop Shape
$_POST as $key => $value using checkboxes
Wrong result when using mycrypt_cfb
how can i make php alert pop ups just like in javascript
Comparing two identical strings returns false in PHP
How to replace text in HTML
setlocale to fr-FR in PHP and number formatting
I have two problems about tcpdf
How to use MySQL functions when building a query with Zend/PDO
Inflector::humanize($key) converts Date of joining TO Date Of Joining

Categories

HOME
office365
ionic2
artificial-intelligence
devexpress
biztalk
mirc
gallery
moodle-api
amp
drag-and-drop
share
u-boot
wagtail
tup
anchor
spring-security-oauth2
lapack
locationmanager
nsstring
bootstrap-switch
grocery-crud
jodatime
webstore
distributed-computing
corona
orange-api
closures
mixpanel
infrared
error-logging
spring-ldap
hdl
chronicle-queue
react-dnd
password-generator
high-availability
echosign
basex
basic-authentication
envoy
phpstorm-2016.1
transform
ftp-server
nsuserdefaults
best-buy-api
task-parallel-library
windows-store
data-uri
hdiv
fiware-wirecloud
sendinput
head.js
solr-query-syntax
gtk#
algebra
gnome-terminal
degrees
adobe-reader
ndk-build
system.web.optimization
system.net.mail
mathml
between
maximize
django-redis
accessibilityservice
outlook.com
boost-proto
lowercase
http.client
replicaset
algebraixlib
textscan
tree-structure
webproject
encryption-symmetric
qt5.4
visual-c++-2012
full-text-indexing
csslint
incognito-mode
cidr
hotswap
armv6
sensormanager
xsd2code
grails-2.3
boinc
xs
days
linkbutton
multiscaleimage
multiple-dispatch
ip-geolocation
android-dialog
popup-blocker
pcspim
flymake
wcf-callbacks
affinetransform
zend-server-ce
django-apps
play2-mini
mediacontroller
solandra
cleartype
database-cloning
index.dat
downloadfile

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App