The keepFirstImgs() function presented in this page can be used in JavaScript to keep the first Nr IMG tags into a string, and Remove all the other <img>s.
- The function receives the number of the first <img> tags to keep (nrimg), and the string with the HTML text (str); returns the string with the other IMG tags removed.
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}
- Example:
<script type="text/javascript">
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}

// Test, keeps the first two IMG tags in $str
var str = 'First img: <img src="img1.jpg" alt="img 1" width="30" />,\
 second image: <img src="img_2.jpg" alt="img 2" width="30">,\
 another Img tag <img src="img3.jpg" alt="img 3" width="30" />,\
 and another image <img src="img_4.jpg" alt="imgage 4" width="30" /> ... etc.';

str = keepFirstImgs(2, str);

alert(str);
/* Result:
 First img: <img src="img1.jpg" alt="img 1" width="30" />,
 second image: <img src="img_2.jpg" alt="img 2" width="30">,
 another Img tag , and another image  ... etc.
*/
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Keep the first Nr IMG tags, Remove all the others

Last accessed pages

  1. Get Duration of Audio /Video file before Upload (5913)
  2. Follow the mouse cursor with a DIV inside a Parent (2703)
  3. Register and show online users and visitors (23278)
  4. Node.js Move and Copy Directory (1127)
  5. SHA1 Encrypt data in JavaScript (8256)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (390)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (332)
  3. Register and show online users and visitors (302)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (234)
  5. Insert, Select and Update NULL value in MySQL (223)