atticus Posted February 19, 2009 Share Posted February 19, 2009 I need to be able to replace something that only occurs in a specific element, but it occurs in more than one element... for example... <h1>A title</h1> <li>A title</li> I want to change <h1>A title</h1> to something different. preg_replace() will not work because the pattern changes often... <?php $string_replace = str_replace($title, 'another title', $buffer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/ Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 $test = "<h1>A title</h1>"; <?php $string_replace = substr_replace($test, 'another title', $buffer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766154 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 <?php $string = "Replace this title <h1 class>Title</h1> <h1>heretoo</h1>"; $replaced = preg_replace("~<h1(.+?)?>(.+?)</h1>~s", '<h1$1>test title</h1>', $string); echo "Original: " . htmlentities($string) . "<br />"; echo "Replaced: " . htmlentities($replaced) . "<br />"; ?> preg_replace will work because it suits/uses patterns, str_replace is the simple one. Try the above and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766156 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 The substr_replace() did not work...$buffer is the entire page's HTML...all that the code displayed was 'another title'. Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766161 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 <?php $string = "Replace this title <h1 class>Title</h1> <h1>heretoo</h1>"; $replaced = preg_replace("~<h1(.+?)?>(.+?)</h1>~s", '<h1$1>test title</h1>', $string); echo "Original: " . htmlentities($string) . "<br />"; echo "Replaced: " . htmlentities($replaced) . "<br />"; ?> It does replace the <h1> and title, however, there may or may not be an <h1 class><a href="">a title</a></h1> The presence of the link throws me off a bit... Just fyi...I am developing a plugin for my wordpress install. Wordpress passes the function everything from <body> to </body>. So $buffer is the entire pages HTML, excluding the <head> section. Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766169 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 The substr_replace() did not work...$buffer is the entire page's HTML...all that the code displayed was 'another title'. Sorry about that, thought it would work, let's see what else we can do... Can you provide some examples of input, and what you expect output wise? Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766173 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 Can you provide some examples of input, and what you expect output wise? Example of input $buffer = <html> <body> <h1><a href="">The Title</a></h1> <div>more HTML stuff</div> <div>more HTML stuff</div> <div>more HTML stuff</div> <div>more HTML stuff</div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766181 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 <html> <body> <h1><a href="">The Title</a></h1> <div>more HTML stuff</div> <div>more HTML stuff</div> <div>more HTML stuff</div> <div>more HTML stuff</div> </body> Ok, that's the input, but what kind of output do you want to do by replacing the H1? Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766184 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 I simply want to replace the text inside of the h1 "The Title" However, "The Title" is also featured inside of the navigation in <li>The Title</li> So, the navigation may say "Home" But I want my title to say "Really Cool Widgets" without replacing the navigation link of home. So output would be <body> <h1>Really Cool Widgets</h1> <div>navigation...<li>The Title</li></div> etc. Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766191 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 ohh and one more thing...every page does not use the same format...since it is a plugin...it has to work with any variation of <h1>The title</h1> such as <h1 class> <h1 class><a href> Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766194 Share on other sites More sharing options...
atticus Posted February 19, 2009 Author Share Posted February 19, 2009 alright...I used this and it replaced the title and kept the <a href in place...however it left out the closing</a> Any suggestions to improve this code to include the </a> if it should exist... Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766203 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 ohh and one more thing...every page does not use the same format...since it is a plugin...it has to work with any variation of <h1>The title</h1> such as <h1 class> <h1 class><a href> Ok slow down there big shifter. It can be solved, but you are adding stuff to it by the minute. First it was a simple <h1> and then it is <h1><a href> and now it is <h1 class>. Here are some guidelines I suggest you follow when creating a topic. 1. State the problem clearly: I want to replace the title that is contained within <h1> tags, where the h1 tag can have attributes and there is the possability of inner tags also being inside these tags. 2. Show an example(s) of different data with desired output: For example: <h1>This title</h1> should be <h1>replaced title</h1> <h1 class="bob">this title </h1> should be <h1 class="bob">replaced title</h1> <h1><a href="thispage.php">this title</a></h1> should be <h1><a href="title.php">replaced title</a></h1> 3. Show what you have tried: I have tried using str_replace here is some code. I do not think preg_replace will work, but I do not know enough about regex to make it work: <?php $string_replace = str_replace($title, 'another title', $buffer); ?> I would appreciate any help. That is a much better mock up, as giving your original post this was solved. Now before I go hog wild on helping you any further with regex answer these questions for me: Do you know the title you want replaced? Can you find that out at all? This is important as it can be very helpful to solving your issue easier. Is there more than 1 possibility of <h1> being on the page? Let me know and I will post on items that may help you/solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/145940-is-there-a-way-to-replace-a-stringunknown-characterstitle/#findComment-766209 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.