SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP String Extract

We can use php built in string function to create a function which can be used in various applications to collect part of a string. Here there will be one main string. We will supply one starting landmark or staring point and one ending landmark point or ending point. The string in between these two mark points we can extract and use in our application. We will be using two important functions stristr and substr to develop this function.

We can use this function in different applications. For example let us say we want to extract all names in one xml document.

<type>PHP programmer</type>
<name>Your name is here</name>


Here we can use <name> and </name> as two landmark points to extract the name from the above string.

There are two functions. One returns the part sting without landmark points and other one includes both landmark points. Here is the code.
<?
function my_strip($start,$end,$total){

$total = stristr($total,$start);

$f2 = stristr($total,$end);

return substr($total,strlen($start),-strlen($f2));

}

function my_strip2($start,$end,$total){ // includes both landmarks

$total = stristr($total,$start);

$f2 = stristr($total,$end);

return substr($total,0,(strlen($total)-(strlen($f2) - strlen($end))));

}
/////////////////////

$string="You are welcome to plus2net.com. Use this site for your learning of web programming and design , php is one of the programming language";

$contents=my_strip("welcome","programming",$string);
echo $contents;
echo "<br>";
$contents=my_strip2("welcome","programming",$string);
echo $contents;

?>


The first function will display part of the string without the landmarks ( welcome and programming ) but second function will keep them at starting and at end.

Read how these functions are used to read the text within title tags of page

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
String Functions
htmlspecialchars
md5 hash
nl2br
Random string
strip_tags
str_replace
str_ireplace
strlen
strrev
stristr
split
substr
substr_count
str_repeat
strtolower
strcasecmp
strcmp
str_pad
trim
ucwords
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.