Jump to content

String matching: Difference between revisions

PHP added
No edit summary
(PHP added)
Line 1,271:
R_find: 0 1 6 Nil()
R_find_all: 0 1 3 5 6 Nil() </pre>
 
=={{header|PHP}}==
<lang php><?php
/**********************************************************************************
* This program gets needle and haystack from the caller (chm.html) (see below)
* and checks for occurrences of the needle in the haystack
* 02.05.2013 Walter Pachl
* Comments or Suggestions welcome
**********************************************************************************/
$haystack = $_POST['haystack']; if ($haystack=='') {$haystack='no haystack given';}
$needle = $_POST['needle']; if ($needle=='') {$needle='no needle given';}
 
function rexxpos($h,$n) {
$pos = strpos($h,$n);
if ($pos === false) { $pos=-1; }
else { $pos=$pos+1; }
return ($pos);
}
 
$pos=rexxpos($haystack,$needle);
$tx1 = "";
if ($pos==-1){ $n=0; } // not found
else { $n=1; } // found once (so far)
// Special cases
if ($pos==1){ $tx1="needle found to be the start of the haystack"; }
if ($pos==strlen($haystack)-strlen($needle)+1)
{ $tx1="needle found at end of haystack"; }
 
if ($n>0) { // look for other occurrences
$pl=$pos; // list of positions
$p=$pos; //
$x="*************************************";
$h=$haystack;
while ($p>0) {
$h=substr($x,0,$p).substr($h,$p);
$p=rexxpos($h,$needle);
if ( $p>0 ) { $n=$n+1; $pl=$pl.",&nbsp;".$p; }
}
if ($n==1) { $txt="needle found once in haystack, position: $pl."; }
else if ($n==2) { $txt="needle found twice in haystack, position(s): $pl."; }
else { $txt="needle found $n times in haystack, position(s): $pl."; }
}
else { $txt="needle not found in haystack."; }
?>
<html>
<head>
<title>Character Matching</title>
<meta name="author" content="Walter Pachl">
<meta name="date" content="02.05.2013">
<style>
p { font: 120% courier; }
</style>
</head>
<body>
<p><strong>Haystack:&nbsp;'<?php echo "$haystack" ?>'</strong></p>
<p><strong>Needle:&nbsp;&nbsp;&nbsp;'<?php echo "$needle" ?>'</strong></p>
<p><strong><?php echo "$txt" ?></strong></p>
<!-- special message: -->
<p style="color: red";><strong><?php echo "$tx1" ?></strong></p>
</body>
</html></lang>
<lang php><?php
<!DOCTYPE html>
<!--
/************************************************************************
* Here we prompt the user for a haystack and a needle
* We then invoke program chmx.php
* to check for occurrences of the needle in the haystack
* 02.05.2013 Walter Pachl
* Comments or Suggestions welcome
************************************************************************/
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Character matching</title>
</head>
<body>
<form id="test" name="test" method="post" action="chmx.php">
<h1>Character matching</h1>
<p>Given two strings, demonstrate the following 3 types of matchings:
<ol style="margin-top:2; margin-bottom:2;">
<li>Determining if the first string starts with second string
<li>Determining if the first string contains the second string at any location
<li>Determining if the first string ends with the second string
</ol>
<p>Optional requirements:
<ol style="margin-top:2; margin-bottom:2;">
<li>Print the location of the match(es) for part 2
<li>Handle multiple occurrences of a string for part 2.
</ol>
<p style="margin-top:5; margin-bottom:3;">
<font face="Courier"><strong>Haystack:</strong>
<strong><input type="text" name="haystack" size="80"></strong></font></p>
<p style="margin-top:5; margin-bottom:3;">
<font face="Courier"><strong>Needle:&nbsp;&nbsp;</strong>
<strong><input type="text" name="needle" size="80"></strong></font></p>
<p>Press <input name="Submit" type="submit" class="erfolg" value="CHECK"/>
to invoke chmx.php.</p>
</form>
</body>
</html></lang>
 
=={{header|PicoLisp}}==
2,299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.