Modify html attribute with php
Is there any simplier way for this or should I do it with DOM?
Do it with DOM.
Here's an example:
<?php
$html = '<a href="http://example.com" rel="nofollow external">test</a>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//a[contains(concat(' ', normalize-space(@rel), ' '), ' external ')]");
foreach($nodes as $node) {
$node->setAttribute('href', 'http://example.org');
}
echo $dom->saveHTML();
change html attributes or style using php
You can't. Your best bet is just putting a script tag inside or outside your php page.
Like
...?>
<script></script>
<?php...
or
<?php
echo"<script></script>";
?>
JQuery is NOT the only way you can change styles or attributes, imo native javascript is hell of a lot easier
Set an HTML attribute with PHP
When the user submits the data...
Do you mean there's a $_POST or $_GET request? If so, this would make a little more sense
<input id="username"
name="username"
placeholder="Username"
value="<?php echo isset($_GET['username']) ? $_GET['username'] : '' ?>">
If $_GET['username']
isn't present, you will just get value=""
which is perfectly fine
Writing HTML via PHP by hand is a total chore. To combat this, I've written a utility called htmlgen which makes it quite a bit more tolerable
<?php
use function htmlgen\html as h;
h('input#username', [
'name'=>'username',
'placeholder'=>'Username',
'value'=>isset($_GET['username']) ? $_GET['username'] : ''
]);
// => <input id="username" name="username" placeholder="Username" value="someuser">
What's the best practice to set html attribute via PHP?
You always want to HTML-encode things inside HTML attributes, which you can do with htmlspecialchars
:
<span title="<?php echo htmlspecialchars($variable); ?>">
You probably want to set the second parameter ($quote_style
) to ENT_QUOTES
.
The only potential risk is that $variable
may already be encoded, so you may want to set the last parameter ($double_encode
) to false
.
Php: modify attribute html tag
If I understand correctly you are need something like:
foreach ($dom->getElementsByTagName('option') as $item) {
if ($item->getAttribute('selected') == "selected")
$item->setAttribute("value", "-1");
}
This way you pass on your option items and set the value of the selected ones
Change tag attribute value with PHP DOMDocument
$dom = new DOMDocument();
$dom->loadHTML('<a href="http://foo.bar/">Click here</a>');
foreach ($dom->getElementsByTagName('a') as $item) {
$item->setAttribute('href', 'http://google.com/');
echo $dom->saveHTML();
exit;
}
PHP getting and setting attributes on HTML Elements
// to get the 'src' attribute
$src = $frame->getAttribute('src');
// to set the 'src' attribute
$frame->setAttribute('src', 'newValue');
To change the URL, you should first use parse_url($src)
, then rebuild it with your new query arguments, for example:
$parts = parse_url($src);
extract($parts); // creates $host, $scheme, $path, $query...
// extract query string into an array;
// be careful if you have magic quotes enabled (this function may add slashes)
parse_str($query, $args);
$args['newArg'] = 'someValue';
// rebuild query string
$query = http_build_query($args);
$newSrc = sprintf('%s://%s%s?%s', $scheme, $host, $path, $query);
Related Topics
Why Is My Database Backup Script Not Working in PHP
What Does the "&" Sign Mean in PHP
How to Change the Name of an Element in Dom
Clear Data in MySQL Table with PHP
How to Access the Google Spreadsheets API in PHP
Error 1366 (Hy000): Incorrect String Value: '\Xf0\X9F\X98\X9C' for Column 'Comment' at Row 1
How to Keep Leading Zeros in PHP Integer
Woocommerce: Display Also Product Variation Description on Cart Items
Connect to Azure Active Directory from PHP Web Application
How to Remove Values from an Array in PHP
No Mail Received in Inbox with Xampp 1.8.0, Mercurymail and Mail()
How to Fix Server Status Code: 302 Found by SQL Inject Me Firefox Addon
How to Detect If a User Has Logged Out, in PHP
Zend Framework 2 - Removed Form Element Causes Validation to Fail
How to Avoid Using PHP Global Objects