- substr: documentation ( source)
- strpos: documentation ( source)
<?php
function getAnchorText($txt)
{
$openTag = '#a#';
$closeTag = '#/a#';
$offsetStart = strpos($txt, $openTag);
if(false !== $offsetStart) {
$offsetStart+= strlen($openTag);
}
else {
$offsetStart = 0;
}
$offsetEnd = strpos($txt, $closeTag);
if (false !== $offsetEnd) {
$offsetLength = $offsetEnd - $offsetStart;
}
else {
$offsetLength = strlen($txt);
}
return substr($txt, $offsetStart, $offsetLength);
}
$txt = 'Some text with #a#anchor#/a# end';
echo getAnchorText($txt);