Jump to content

[SOLVED] Find and replace within a string maintaining beginning/end of said string


benmartin

Recommended Posts

I do not like to resort to asking questions in a forum like this, but I am exhausted in trying to find how to do this on my own.

 

I have zero familiarity with RegEx, but it seems that I need it to find a solution. I've never needed it until now.

 

Basic overview:

  we have started using Adobe InDesign and have a number of publications that need to be migrated to the web. The XML (more specifically - xml cross-media) output is full of worthless tags / stylesheet information / classes for every tag, that vary widely depending on the styles used by production for the print product.

 

 

Example:

  These are four different styles for different stories' headlines (the number denotes the font size):

p.head-styles-head-24 {}

p.head-styles-head-26 {}

p.head-styles-head-28 {}

p.head-styles-head-32 {}

 

    here are the headlines, along with the paragraph + class tags (not in the same order, but just to get an idea):

<p class="head-styles-head-32">Pending sdssdasdfas Legislation</p>

<p class="head-styles-head-32">SourcesasfasfsaasPackage</p>

<p class="head-styles-head-24">Firm Blood asdasdasCompetition</p>

                  etc.

 

    What I would like to do is strip everything after class="head-styles  from the < p > tag and add my own identifier (for further parsing)

 

    essentially replacing <p class="head-styles*****All other characters before the quote****"> to something like <p %%%headline%%%>

 

 

    it would be the equivalent of saying (where wild = %)    :

      find the string :      <p class="head-styles%">

      replace that with:    <p **whatever I specify**>  (I would believe that I'd be able to tell how to change what I want in there for other classes that exist, I just need a template).

 

it must be able to check against the "head-styles" because there are other tags like "body-styles".

 

Any help is MUCH appreciated. I've been working with this for days and have made zero progress.

 

 

I really hope this makes sense.

 

 

Thanks.

 

-Ben

Link to comment
Share on other sites

Something like this?

 

<pre>
<?php
$data = <<<DATA
	<p class="head-styles-head-32">Pending sdssdasdfas Legislation</p>
	<p class="head-styles-head-32">SourcesasfasfsaasPackage</p>
	<p class="head-styles-head-24">Firm Blood asdasdasCompetition</p>
DATA;
$data = preg_replace('/(<p[^>]+class="head-styles)[^"]+/', '$1-new_style', $data);
echo htmlspecialchars($data);
?>
</pre>

Link to comment
Share on other sites

Effigy,

 

Thanks for the quick response.

 

Almost but not quite.

 

 

I've modified the code a little bit and I've added the output here as well.

 

----- code now (basically just changed the replace value) ---

 

<pre>

<?php

$data = <<<DATA

<p class="head-styles-head-32">Pending sdssdasdfas Legislation</p>

<p class="head-styles-head-32">SourcesasfasfsaasPackage</p>

<p class="head-styles-head-24">Firm Blood asdasdasCompetition</p>

DATA;

$data = preg_replace('/(<p[^>]+class="head-styles)[^"]+/', '<p myvalue>', $data);

echo htmlspecialchars($data);

?>

</pre>

 

 

 

--------- output

 

EDIT::::

 

Sorry, that is supposed to be :

 

 

<p myvalue>">Pending sdssdasdfas Legislation</p>

 

Not

             <p myvalue>">Pending sdssdasdfas Legislation</p>

<p myvalue>">SourcesasfasfsaasPackage</p>

<p myvalue>">Firm Blood asdasdasCompetition</p>

 

Notice the (edit) "> in green. I would like to have those removed. It is removed from the front however.

 

Aside from that, it's perfect.

 

I appreciate your help very much.

 

-Ben

 

 

 

Link to comment
Share on other sites

Are there no other attributes you need to retain?

 

<pre>
<?php
$data = <<<DATA
	<p class="head-styles-head-32">Pending sdssdasdfas Legislation</p>
	<p class="head-styles-head-32">SourcesasfasfsaasPackage</p>
	<p class="head-styles-head-24">Firm Blood asdasdasCompetition</p>
DATA;
$data = preg_replace('/<p[^>]+class="head-styles[^>]+>/', '<p myvalue>', $data);
echo htmlspecialchars($data);
?>
</pre>

Link to comment
Share on other sites

No, that's all I was looking for.

 

What I will be looking for now is a good tutorial on this for the future. It seems like it can be pretty powerful.

 

 

Effigy, thanks so much for this. I am very grateful that you could help me.

 

 

Take care,

-Ben

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.