Php-mysql Course

The keepFirstImgs() function presented in this page can be used in PHP to keep the first Nr IMG tags into a string, and Strip 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 strip all the other <img>s
// From: https://coursesweb.net/php-mysql/
function keepFirstImgs($nrimg, $str) {
  // gets an array with al <img> tags from $str
  if(preg_match_all('/(\<img[^\>]+\>)/i', $str, $mt)) {
    // gets array with the <img>s that must be stripped ($nrimg+), and removes them
    $remove_img = array_slice($mt[1], $nrimg);
    $str = str_ireplace($remove_img, '', $str);
  }
  return $str;
}
- Example:
<?php
// Function to keep first $nrimg IMG tags in $str, and strip all the other <img>s
// From: https://coursesweb.net/php-mysql/
function keepFirstImgs($nrimg, $str) {
  // gets an array with al <img> tags from $str
  if(preg_match_all('/(\<img[^\>]+\>)/i', $str, $mt)) {
    // gets array with the <img>s that must be stripped ($nrimg+), and removes them
    $remove_img = array_slice($mt[1], $nrimg);
    $str = str_ireplace($remove_img, '', $str);
  }
  return $str;
}

// Test, keeps the first two IMG tags in $str
$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_5.jpg" alt="imgage 5" width="30" /> ... etc.';
$str = keepFirstImgs(2, $str);
echo $str;
/* Output:
 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.
*/
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Keep the first Nr IMG tags, Strip all the others

Last accessed pages

  1. Understanding OOP - Object Oriented Programming (5200)
  2. $_GET, $_POST and $_REQUEST Variables (33892)
  3. Display multiple groups of images (5455)
  4. CSS Trapezoid Shape (11410)
  5. Simple Laravel MySQL CRUD Example (1814)

Popular pages this month

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