/    Sign up×
Community /Pin to ProfileBookmark

using stripos() for multiple search?

I’m using stripos() to find <h1></h1> contents and it works, the problem comes when there’s more than one <h1></h1> pair of tags.
How can I find them all and count them?
I guess I have to move the pointer all around the string, but don’t know how to and the function just returns the first pair of tags.
Any hints?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@TheRaveFeb 14.2008 — Adjust the start parameter.

e.g. If the first pair of tags is found at 53 then run the function again but set it to have a start of 54.
Copy linkTweet thisAlerts:
@PerfidusauthorFeb 14.2008 — How can I just count the coincidences?

If I know how many times the headings appear then I can make a bucle to catch them all so I can move the pointer as much times as I need.
Copy linkTweet thisAlerts:
@scragarFeb 14.2008 — [code=php]function arindex($haystack, $needle){
$a = array();
$latest = stripos($haystack, $needle);
while($latest !== false){
$a[] = $latest;
$latest = stripos($haystack, $needle, $latest+1);
};
return $a;
};

echo count(arindex('cheese', 'e')).' letter "e"s in "cheese" and they are:';
print_r(arindex('cheese', 'e'));[/code]
Copy linkTweet thisAlerts:
@PerfidusauthorFeb 14.2008 — I was just trying your function and i get following error:

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 35 bytes) in /home/chs/dom.com/home/html/x/countandread.php on line 9

[code=php]
function stripos($haystack, $needle){
return strpos($haystack, stristr( $haystack, $needle ));
}
function arindex($haystack, $needle){
$a = array();
$latest = stripos($haystack, $needle);
while($latest !== false){
$a[] = $latest;
$latest = stripos($haystack, $needle, $latest+1);
};
return $a;
};

echo count(arindex('cheese', 'e')).' letter "e"s in "cheese" and they are:';
print_r(arindex('cheese', 'e'));
[/code]
Copy linkTweet thisAlerts:
@PerfidusauthorFeb 14.2008 — I have developed this function starting from another I found in PHP manual, it should find substrings between all the pairs of given substrings
[code=php]
function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
$number=substr_count($inputstr, $delimeterLeft);
$adding=0;
for ($k = 0; $k < $number; $k++) {
$posLeft = stripos($inputstr,$delimeterLeft,$adding)+strlen($delimeterLeft);
$posRight = stripos($inputstr,$delimeterRight,$adding);
$values=array();
$values[]= substr($inputstr,$posLeft,$posRight-$posLeft);
$adding=$adding+$posRight+strlen($delimeterRight);

}
return $values;
}[/code]

I'm trying the function with a string in which there are two coincidences, but I only get the first one...

Can't see why it is ignoring the second one!!
Copy linkTweet thisAlerts:
@andre4s_yFeb 15.2008 — Why you do not try using regex function??

and still using string function...

preg_match_all() function maybe the answer of your problem, if i am not misunderstand your problem.
Copy linkTweet thisAlerts:
@mehereAug 21.2008 — The function listed by Perfidus works fine. THe reason you are only getting one response is that he is setting the VALUES variable inside the for loop. Put the statement $values=array(); outside the loop and all is well. Hope this helps someone. Here is the script edited:

function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {

$number=substr_count($inputstr, $delimeterLeft);

$adding=0;

$values=array();

for ($k = 0; $k < $number; $k++) {

$posLeft = stripos($inputstr,$delimeterLeft,$adding)+strlen($delimeterLeft);

$posRight = stripos($inputstr,$delimeterRight,$adding);

$values[]= substr($inputstr,$posLeft,$posRight-$posLeft);

$adding=$adding+$posRight+strlen($delimeterRight);

}

return $values;

}
×

Success!

Help @Perfidus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...