Jump to content

set attribute to a tag


raduenea

Recommended Posts

Hello,

 

I have the following code: 

$html = file_get_html('https://domain.com/.../..../');
foreach($html->find('div[id=container_tab3]') as $div) {
	
	$tab3 = $div->innertext . '<br><br>';

}

$tab3 variable have some html like:

<div  ><span >text text</span><p>......

What can I do to modify $tab3 in order to add an attribute to span tag to become: <span style="font-weight:bold;>   ?

 

Thanks

 

 

 

Edited by raduenea
Link to comment
Share on other sites

There are lots of ways to skin this cat.  A trick I often use:

$style = "";

if ($bolding_condition) {
 
    $style = "style='font-weight:bold;'";
}
 
echo "<span $style>" . $content . "</span>\n";
 

Of course, that yields inline CSS, which bothers some people.  You could also append "<b>" or "<strong>" tags and the corresponding closure tags at the beginning and ended of $content if the condition matched.  Or, you can set a specific class if the condition matches true.  OR, you can wait under you're done and echo a JavaScript snippet that does what you want if the condition is matched.
Link to comment
Share on other sites

Since you are using this library, you need to read the manual: http://simplehtmldom.sourceforge.net/manual.htm

 

Something like this might work:

 

$html = file_get_html('https://domain.com/.../..../');
foreach($html->find('div[id=container_tab3]') as $div) {
    $tab3 = $div->innertext . '<br><br>';
    foreach ($div->find('span') as $span) {
        $span->style = 'font-weight:bold;';    
    }
}
Link to comment
Share on other sites

Since you are using this library, you need to read the manual: http://simplehtmldom.sourceforge.net/manual.htm

 

Something like this might work:

 

$html = file_get_html('https://domain.com/.../..../');
foreach($html->find('div[id=container_tab3]') as $div) {
    $tab3 = $div->innertext . '<br><br>';
    foreach ($div->find('span') as $span) {
        $span->style = 'font-weight:bold;';    
    }
}

That's a good start. 

It's there a way to add "style" only to the first "span" tag ?

Idea it's that I have many span tags and I want to add "Style" only to the first one.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.