Javascript Course

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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Keep the first Nr IMG tags, Remove all the others

Last accessed pages

  1. Graphic Symbols (1512)
  2. CSS cursor property - Custom Cursors (6184)
  3. Responses (329)
  4. Add sounds and audio effects in Flash (3540)
  5. Align DIVs on the same line (8477)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)