PHP – Strings

Hits: 35

PHP – Strings

 

A string in PHP is a simple character concatenation like “Hello world!”, “Lorem Ipsum…” etc. We already learned about how to set a string in PHP using double and single quotesnowdoc and heredoc syntax.

Strings are one of the most used types of data. The simplest way to assign string values is to wrap it into single or double quotes like follows:

PHP Code:
<?php
	$a = 'Simple string value';	 
	$b = "Another string value using double quotes.";

	echo $a;
	echo "<br />";
	echo $b;
?>

Escaping quotes in strings

Simple or double quotes are many times found in blocks of text. You can escape quotation marks using the backslash () symbol.

PHP Code:
<?php
	$a = 'This is how I'll be escaping a single quotation mark in PHP.';
	echo $a;
?>

 

Nowdoc and heredoc syntax

There are also other ways to define string values. Besides single and double quotation you can use heredoc and nowdoc syntax if you run PHP 5.3.0 or later. Here is a heredoc example:

PHP Code:
<?php
$here = <<<EOT
    I'm here to show you how to use heredoc syntax!
EOT;

	echo $here
?>

Nowdoc syntax similar to heredoc, but just like single quotes, it doesn’t allow using and evaluating variable inside it. Here is a side by side example so you can compare it:

PHP Code:
<?php
$foo = 'bar';

// heredoc example
$here = <<<EOT
    I'm here, $foo!
EOT;

// Nowdoc example
$now = <<<'EOT'
    I'm now, $foo!      
EOT;

echo $here // prints I'm here, bar!
echo $now  // prints I'm now, $foo!
?>

Limits on string type data

According to the PHP manual: (http://php.net/manual/en/language.types.string.php) “as of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)”.

String manipulation using PHP built-in functions

PHP has lots of built-in function that helps you when it comes to working with strings. You can:

  • count characters or words and reverse character order of a string
  • search, search and replace inside the string, split it into multiple strings by a certain character, etc
  • transform string to uppercase, lowercase, capitalize remove leading and trailing white spaces and so on
  • obtain a hash from a given string, convert html tags to entities so you can print the symbols instead of interpret them.

 

This are just a few of the PHP function, You can check the full reference list of them in the PHP string functions reference page.

PHP count characters, words and reverse order

This is how we count characters and words in PHP. We will use the strlen() and str_word_count() respectively.

PHP Code:
<?php
	$string = 'Working with string in PHP is awesome!';

	echo "This string has ".strlen($string). " characters and ". str_word_count($string). " words.";
	echo "<br />Now try to spell it in reverse order :-)<br />";
	echo strrev($string);
?>

PHP search text occurrence in string

strpos() PHP function searches for a given text inside a string. It will return the first character position of the first match and FALSE if it didn’t find it.

PHP Code:
<?php
	echo strpos('Working with string in PHP is awesome!', "PHP"); // outputs 23
?>

Here is a really useful example from the real-world coding. You will have to use the identical operator (===) to obtain the expected result. You will learn about it in the next tutorial

PHP Code:
<?php
	$string = 'The quick brown fox jumps over the lazy dog.';
	$search   = 'lazy';
	$pos = strpos($string, $search);

	if ($pos === false) {
	    echo "We didn't found '$search' inside '$string'";
	} else {
	    echo "We located '$search' inside '$string' at position $pos";
	}
?>

PHP search and replace

Use the str_replace() to perform the search and replace job.

PHP Code:
<?php
	$string = 'The quick brown fox jumps over the lazy dog.';
	echo str_replace("brown", "pink", $string); 
?>

str_replace() has a forth param that counts the number of replacements that has been done: str_replace(search, replace, $string, $count)

PHP split string using explode function

The PHP explode() function will split the string into an array every time the separator character it is found. The result will be an indexed array:

PHP Code:
<?php
	$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
	$pieces = explode(" ", $pizza);

	echo "<pre>";
	print_r($pieces);
	echo "</pre>";
?>

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars