raduenea Posted March 25, 2018 Share Posted March 25, 2018 (edited) 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 March 25, 2018 by raduenea Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 26, 2018 Share Posted March 26, 2018 Have you tried echoing the $tab3 variable inside the open <span> tag? The echo statement would go before the greater than (>) symbol. Note that you will need to remove the <br> tags, which are added to $tab3 inside the foreach loop. Quote Link to comment Share on other sites More sharing options...
dalecosp Posted March 26, 2018 Share Posted March 26, 2018 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 26, 2018 Share Posted March 26, 2018 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;'; } } Quote Link to comment Share on other sites More sharing options...
raduenea Posted March 26, 2018 Author Share Posted March 26, 2018 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.