Skip to content

Instantly share code, notes, and snippets.

@tiede
Last active August 29, 2019 11:29
Show Gist options
  • Select an option

  • Save tiede/30ea2056d45cbe17f0f3cfb0d42fe633 to your computer and use it in GitHub Desktop.

Select an option

Save tiede/30ea2056d45cbe17f0f3cfb0d42fe633 to your computer and use it in GitHub Desktop.
<?php
$welcomeText='<p>Hello MMA</p>';
?>
<?=$welcomeText;?>
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<questions>
<question text='Do you like beer?'>
<answer text='YES!' />
</question>
</questions>
XML;
$questions = new SimpleXMLElement($xmlstr);
?>
<p>Spørgsmål : <?=$questions->question[0]['text']?></p>
<p>Svar : <?=$questions->question[0]->answer['text']?>
<?php
$pattern = "/ca[kf]e/";
$text = "He was eating cake in the cafe.";
if(preg_match($pattern, $text)){
echo "Match found!";
} else{
echo "Match not found.";
}
?>
<?php
$pattern = "/ca[kf]e/";
$text = "He was eating cake in the cafe.";
$matches = preg_match_all($pattern, $text, $array);
echo $matches . " matches were found.";
?>
<?php
$pattern = "/\s/";
$replacement = "-";
$text = "Earth revolves around\nthe\tSun";
// Replace spaces, newlines and tabs
echo preg_replace($pattern, $replacement, $text);
echo "<br>";
// Replace only spaces
echo str_replace(" ", "-", $text);
?>
<?php
$pattern = "/[\s,]+/";
$text = "My favourite colors are red, green and blue";
$parts = preg_split($pattern, $text);
// Loop through parts array and display substrings
foreach($parts as $part){
echo $part . "<br>";
}
?>
<?php
$pattern = "/^J/";
$names = array("Jhon Carter", "Clark Kent", "John Rambo");
$matches = preg_grep($pattern, $names);
// Loop through matches array and display matched names
foreach($matches as $match){
echo $match . "<br>";
}
?>
<?php
$pattern = "/color/i";
$text = "Color red is more visible than color blue in daylight.";
$matches = preg_match_all($pattern, $text, $array);
echo $matches . " matches were found.";
?>
@useerup
Copy link
Copy Markdown

useerup commented Aug 29, 2019

Total Perspective Vortex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment