Viewed   170 times

I need to parse an HTML document and to find all occurrences of string asdf in it.

I currently have the HTML loaded into a string variable. I would just like the character position so I can loop through the list to return some data after the string.

The strpos function only returns the first occurrence. How about returning all of them?



Without using regex, something like this should work for returning the string positions:

$html = "dddasdfdddasdffff";
$needle = "asdf";
$lastPos = 0;
$positions = array();

while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
    $positions[] = $lastPos;
    $lastPos = $lastPos + strlen($needle);

// Displays 3 and 10
foreach ($positions as $value) {
    echo $value ."<br />";
Sunday, October 2, 2022

You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
    url: "ajax.php",
    type: "post",
    data: jsString

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022

alert('my name is: <?php echo $man; ?>' );

Friday, August 19, 2022

try the following

const std::string s = "*A";
const std::string t = "*An";

std::string::size_type n = 0;
while ( ( n = chartDataString.find( s, n ) ) != std::string::npos )
    chartDataString.replace( n, s.size(), t );
    n += t.size();
Tuesday, December 13, 2022

One way to do this is to find the indices using list comprehension:

currentWord = "hello"

guess = "l"

occurrences = currentWord.count(guess)

indices = [i for i, a in enumerate(currentWord) if a == guess]

print indices


[2, 3]
Friday, September 2, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :