Category: PHP String Functions
PHP helper function to replace placeholders in string
Below is the PHP helper function which can be use to replace placeholders in a specified string with their actual values:
function replace_placeholders($text, $placeholders = []) {
$placeholderPrefix = ':';
foreach ($placeholders as $key => $value) {
$text = str_replace($placeholderPrefix.$key, $value, $text);
}
return $text;
}
Usage:
$text = 'Learn beginners, intermediate and advance level :LANGUAGE and associated technologies by :NAME.';
$placeholders = ['NAME' => 'PHP Tutors', 'LANGUAGE' => 'PHP'];
echo replace_placeholders($text, $placeholders);
//Output:
Learn beginners, intermediate and advance level PHP and associated technologies by PHP Tutors.
Description:
$text
is the parameter containing string in which we have placeholders:LANGUAGE
and:NAME
in the above example.$placeholders
is the parameter containing placeholders as an array key and actual values as an array values.- Using
str_replace()
PHP’s function we replace placeholders with their actual values like:LANGUAGE
withPHP
and:NAME
withPHP Tutors
as shown in above example.
Prevent Warning Invalid argument supplied for foreach
<?php
class Post{
public $tag;
/*
other logic ...
*/
}
$post = new Post();
foreach(is_array($post->tag)?$post->tag:array() as $val) {
echo "PHP Tutors";
/*
other logic ...
*/
}
?>
Get help from PHP Tutors using Skype
Skype video and voice messaging service is awesome now you can send your questions by using Skype video or voice message to PHP Tutors.
Add us on Skype.
Skype Name: phptutors
Regards,
PHP Tutors,
Thankyou
PHP chunk_split() Function Example
PHP Script:
<?php
echo chunk_split(“PHPTUTORS”,1,”_”);
echo “<br/ >”;
echo chunk_split(“Learn PHP on PHPTUTORS”,12,”…”);
?>
Output:
P_H_P_T_U_T_O_R_S_
Learn PHP on… PHPTUTORS…
Description:
chunk_split() function splits a string into a series of smaller chunks and append a specified string after each chunk as shown in above script example.
chunk_split function parameters:
1- String: String which is going to be spit into chunks
2- Chunk Length: Length of chunk [default is 76]
3- String: String which is appended at the end of each chunk
PHP parse_str() Function Example
PHP Script:
<?php
parse_str(“post=PHP%20parse%20str&blog=PHPTUTORS”);
echo $post;
echo “<br / >”;
echo $blog;
echo “<br / >”;
parse_str(“post=PHP%20parse%20str%20function&blog=PHPTUTORS”,$data_array);
print_r($data_array);
?>
Output:
PHP parse str
PHPTUTORS
Array ( [post] => PHP parse str function [blog] => PHPTUTORS )
Description:
parse_str() function parses a query string into variables.
parse_str function parameters:
1- String: Query string which is going to be parse into variables as shown in above script example.
2- Array: This parameter tells that query string data will be stored in an associative array as shown in above script example.
PHP strstr() Function Example
PHP Script:
<?php
echo strstr(“PHP str str function example on PHPTUTORS”,”PHPTUTORS”);
?>
Output:
PHPTUTORS
Description:
strstr() function searches for the first occurrence of a string inside another string.
This function returns the string from the matching point like PHPTUTORS in above example script or if the string is not found false will be return.
This function is case-sensitive.
strstr function parameters:
1- String: string to search.
2- Search string: Specify the string to search for. We can also provide a number so, characters ASCII value matching is performed with this number.
PHP str_repeat() Function Example
PHP Script:
<?php
echo str_repeat(“:.:”,10);
?>
Output:
:.::.::.::.::.::.::.::.::.::.:
Description:
str_repeat() function repeats the string to a specified number of times.
str_repeat function parameters:
1- String: string to repeat.
2- Repeat: Specify how many times the above string to repeat [0 or more].
PHP substr_count() Function Example
PHP Script:
<?php
echo substr_count(“PHPTUTORS a platform for learning PHP”,”PHP”);
echo “<br >”;
echo substr_count(“PHPTUTORS a platform for learning PHP”,”PHP”,33,3);
?>
Output:
2
1
Description:
substr_count() function counts the number of times a substring occurs in a string.
substr_count function parameters:
1- String: String containing substring to search for.
2- Substring: Substring which is going to be search in above string.
3- Start: From where to start searching a substring.
4- Length: Start search from above specified start position and end search after this LENGTH completion.
For e.g. in the above example 33 is the starting position of search and 3 is the length of the search so with in this boundary substring search.
PHP strrev() Function Example
PHP Script:
<?php
echo strrev(“PHP TUTORS”);
?>
Output:
SROTUT PHP
Description:
strrev() function use to reverse a string.
strrev function parameters:
1- String: string to reverse
PHP strip_tags() Function Example
PHP Script:
<?php
echo strip_tags(“<b >PHP TUTORS </b >”);
echo “<br >”;
echo strip_tags(“<i > <b >PHP TUTORS </b > </i >”,”<b >”);
?>
Output:
PHP TUTORS
PHP TUTORS
Description:
strip_tags() function strips tags of HTML, XML or PHP from string.
strip_tags function parameters:
1- String: string from which tags are strip
2- Allowed tags: Specifies allowable tags. These tags will not be removed