Get Content Within a HTML Tag Using PHP and Replace It After Processing

Get content within a html tag using php and replace it after processing

esafwan - you could use a regex expression to extract the content between the div (of a certain id).

I've done this for image tags before, so the same rules apply. i'll look out the code and update the message in a bit.

[update] try this:

<?php
function get_tag( $attr, $value, $xml ) {

$attr = preg_quote($attr);
$value = preg_quote($value);

$tag_regex = '/<div[^>]*'.$attr.'="'.$value.'">(.*?)<\\/div>/si';

preg_match($tag_regex,
$xml,
$matches);
return $matches[1];
}

$yourentirehtml = file_get_contents("test.html");
$extract = get_tag('id', 'content', $yourentirehtml);
echo $extract;
?>

or more simply:

preg_match("/<div[^>]*id=\"content\">(.*?)<\\/div>/si", $text, $match);
$content = $match[1];

jim

PHP How to replace html tags with values

You could make use of the DOMDocument API, by creating a document, loading your HTML into it, looking for desired nodes using XPath then replacing them with the newly created link elements:

$domDocument = new \DOMDocument();
$domDocument->loadHTML(utf8_decode($markup), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOWARNING | LIBXML_NOERROR);
$domDocument->encoding = 'utf-8';

$xpath = new DOMXPath($domDocument);

/** @var DOMElement $numElement */
foreach ($xpath->query('//num') as $numElement) {
$linkElement = $domDocument->createElement('a');
$linkElement->setAttribute('href', "https://link-here.php?id={$numElement->textContent}");
$numElement->parentNode->replaceChild($linkElement, $numElement);
$linkElement->appendChild($numElement);
}

$newMarkup = $domDocument->saveHTML();

Demo: https://3v4l.org/XJ8Zi

Note: using regular expressions is not recommended to parse XML/HTML.

Search and replace html element from string

Use the below regex and then replace the match with an empty string.

(?s)(^|\n)?<form\b.*?<\/form>

DEMO

Explanation:

(?s)                     set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
( group and capture to \1 (optional):
^ the beginning of the string
| OR
\n '\n' (newline)
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
<form '<form'
\b the boundary between a word char (\w) and
something that is not a word char
.*? any character (0 or more times)
< '<'
\/ '/'
form> 'form>'

How to get perticular content from html tag attribute into php

Assign an id (test) to image element and with jquery you could fetch src attribute of image with below code.

Jquery

var srcImage = $("#test").attr("src");

Or with Javascript

var srcImage = document.getElementById("test").getAttribute("src");

Now you could perform string operation on srcImage if you want particular part of src

JSFiddle
https://jsfiddle.net/aq0a9jqs/

Sorry Jazz I did not notice that it was exclusively for php.
Kindly refer to question tagged by Quentin as reference. It provides multiple options on how to load/parse html content in php.

There are multiple ways to parse html from string/file/url. Once parsing is performed you could fetch elements from parsed content with supported functions. After referring to answers in tagged question I found SimpleHTMLDOMParser easy to use.

You could you it as below for your problem in particular.

Load HTML from file/URL

$html = file_get_html('http://www.example.com/'); //Your HTML url or file 

Find image with attribute id=test

$ret = $html->find('img[id=test]'); //this will work if image element has ID if you can not change html then use foreach method mentioned in documentations.

Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)

$value = $ret->src;

This is what I understood by referring to documentations here. I have not tested it. http://simplehtmldom.sourceforge.net/ and
http://simplehtmldom.sourceforge.net/manual.htm#section_find

Give it a try and see how it works out. Also refer to other possibilities from tagged question and choose whichever is easy for you. I hope it helps you. lmk.

How to replace string in the website after PHP have complete the processing

I think it's all meaningless, but it works

 document.documentElement.innerHTML = 
document.documentElement.innerHTML.replace(/cat/g,'dog')

About codeigniter

function index() {
$data['demo_data'] = $demo_data;
// Add 3rd argument as true to get output muffer
$text = $this->load->view('test',$data, true);
// Any manipulations with text
echo $text;
}

Replacing inside body using PHP

  1. Split head & body
  2. Replace string in body
  3. Join head & body

Your regex was problematic, I assumed you wanted to catch both https://www.example.com and https://example.com

Here is what you are looking for:

<?php

$homepage = '<head><link rel="stylesheet" type="text/css" href="https://www.example.com/whatever.css"></head><body>This link <a href="https://www.example.com">https://example.com</a> and this one <a href="https://www.example.com">https://www.example.com</a> will be replaced</body>';

$neck_pos = strpos($homepage, "<body");
$head = substr($homepage, 0, $neck_pos);
$body = substr($homepage, $neck_pos);

$body = preg_replace("/https:\/\/[w]*\.*example\.com/", "https://www.example.net", $body);

$homepage = $head . $body;

echo $homepage;


Related Topics



Leave a reply



Submit