Jump to content

Is there a way to replace a string...<h1>(unknown characters)title</h1>


atticus

Recommended Posts

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);
?>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

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.