Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Preg_replace regex

Hi Teachers students, How can I replace content inside a nested div using preg_replace or anything I know about the domDocument but if this is doable using regex plz let me know and please help :)

Or if I can delete all content after a matching string like if it is div id="content"> delete everything after it

Thanks in advance :)

Example:

<div id="content">
 <div id="moreDivs">More Content </div>
<div>  More Content </div>
</div>

2 Answers

Matt Trask
Matt Trask
10,027 Points

You basically answered your own question. Look at preg_replace on PHP.net, it gives you everything you need. You take a regex statement, and then you tell the function where to look so it can replace the values you want to replace.

I actually solved it not using preg_replace because I have no idea why preg_replace never worked with me. What I did is I used substr and strpos to show only a part of the content and delete the rest that was the easiest and best solution in my scenario!

Now I am facing another problem but I know it is my fault I should rework on the database design instead of running in circles trying to solve things the hard way now the problem is I need to get the img src using php and delete everything else in the content and just show the img src! I can do these things using jQuery so easily I know but I want to do it with PHP because I am dealing with database and I don't want to get everything from the database and then do it the jQuery way coz only logged in users will have access on the whole content!

Anyway Thank you so much for the answer :)

Hi Reiyad Alkhatib, great to hear you figured it out, but can you post in the code you used to solve your problem so it can help other people with the same or a similar issue?

Also, it might be useful for your other problem to post a new question in the forums so you can get a more targeted answer!

Yes Sure I can Lets say I have content like this

<div id="content">
 <div id="allowed"> 
  DATA you can see
 </div> <!-- Allowed Content -->
<div id="restricted">
 DATA you can't see
 <div class="moreDivs">Data</div>
<div class="anotherDiv">Data</div>
 </div> <!-- Restricted -->

</div> <!-- Content Ends here -->

Let's assume all the content above have a variable name $content because I was using cms and there is html in the database content table and I didn't want to show content after a specific div id for guests

$content = $this->fulltext; //from database
if($user->guest){
$content = substr($content, 0, strpos($content, '<div id="restricted">'));
$content .= '<span class="readMore">Please login to read more..</div>';  //concatinating
$content .= '</div>'; //closing the content div
} else {
 $content = $this->fulltext;
}

echo $content;

Hope this was helpful