is there a way to tell the occurence postition of the regex given ??

for example ..
$text = 'PHP Hypertext Preprocessor';

if (preg_match('xt', $text)){
echo 'Found Match';
}

Like in that example occurence position is 11.. I already know about the strpos() .. but that doesnt accept regex .. .

What im trying to do is take user input form a form and filter it ... convert everything to htmlspecialchars ... but only allow <object > </object> tags , and everything in between to stay intact .. &quot; <object width=""></object>&quot; tags...

Any help would be appreciated.

Recommended Answers

All 3 Replies

<?php
$subject = "PHP Hypertext Preprocessor";
$pattern = '/xt/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print "<pre>";
print_r($matches);
print "</pre>";
?>

$matches[1] returns 11.
http://in2.php.net/manual/en/function.preg-match.php
It works but I didn't understand the concept of flags !

Cool . Thanx .. I actuially realized i didnt even need the occurence postion to do that ..
this is what i did from the i dea you gave me ..

<?

	  $subject = '<a href="http://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid=35913278">Full Court Shot</a><br/><object width="430px" height="386px"><param name="wmode" value="transparent"/><param name="allowscriptaccess" value="always"/><param name="movie" value="http://lads.myspace.com/videos/vplayer.swf"/><param name="flashvars" value="culture=en-US&a=0&ap=0&y=0&m=35913278&userid=-1&showmenus=0&remove=0&t=&type=video"/><embed src="http://lads.myspace.com/videos/vplayer.swf" width="430" height="386" flashvars="culture=en-US&a=0&ap=0&y=0&m=35913278&userid=-1&showmenus=0&remove=0&t=&type=video" type="application/x-shockwave-flash" allowscriptaccess="always" /></object>';




      $pattern = '/\<object.{1,700}\<\/object\>/';

      preg_match($pattern, $subject, $matches);
		
      $new_data = preg_split($pattern, $subject);
//      print_r($new_data);
      
      
      $article = htmlspecialchars($new_data[0]) . "<br>\n" . $matches[0][0] . "<br>\n" . htmlspecialchars($new_data[1]);
      echo $article;
      
      
//
?>

its the closest thing to perfect that i was able to think of .. Thnax

Great! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.