PHP String Extract

Extract text between two landmarks

When extracting text between markers, first confirm that both markers exist and that the ending marker comes after the starting marker. The helper below uses strpos() and substr() defensively.

function between(string $text, string $start, string $end): ?string {
    $from = strpos($text, $start);
    if ($from === false) return null;
    $from += strlen($start);
    $to = strpos($text, $end, $from);
    if ($to === false) return null;
    return substr($text, $from, $to - $from);
}
The first function returns text between the markers. The second also includes the markers. Both handle missing markers without warnings and search case-insensitively:
function my_strip(string $start, string $end, string $text): ?string {
    $from = stripos($text, $start);
    if ($from === false) return null;
    $from += strlen($start);
    $to = stripos($text, $end, $from);
    if ($to === false) return null;
    return substr($text, $from, $to - $from);
}
function my_strip2(string $start, string $end, string $text): ?string {
    $from = stripos($text, $start);
    if ($from === false) return null;
    $to = stripos($text, $end, $from + strlen($start));
    if ($to === false) return null;
    return substr($text, $from, $to + strlen($end) - $from);
}
$text = "You are welcome to plus2net.com. Learn web programming and PHP here.";
echo my_strip('welcome', 'programming', $text) ?? 'Markers not found';
echo "\n";
echo my_strip2('welcome', 'programming', $text) ?? 'Markers not found';
The first helper omits the markers (welcome and programming); the second retains them. If either marker is missing or out of order, each helper returns null.

We have used the 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. These examples use stripos() for case-insensitive marker positions and substr() for extraction.

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>
For a simple text illustration, use <name> and </name> as markers. For real XML documents, use an XML parser rather than string matching, because attributes, namespaces and nested elements can break delimiter-based extraction.



Read how these functions are used to read the text within title tags of page
String Functions substr(): Extract by position String searching functions trim(): Remove end whitespace


Subscribe to our YouTube Channel here



plus2net.com







RottNKorpse

19-09-2009

Thanks a lot for this...exactly what I was looking for.
elden walden

30-01-2010

great info-will use to change data in classified ad app.
prakash

07-01-2013

very good information really it helped me.




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer