0

i have a string that has markers and I need to replace with text from a database. this text string is stored in a database and the markers are for auto fill with data from a different part of the database.

$text = '<span data-field="la_lname" data-table="user_properties">
{Listing Agent Last Name}
</span>
<br>RE: The new offer<br>Please find attached....'

if i can find the data marker by: strpos($text, 'la_lname'); can i use that to select everything in and between the <span> and </span> tags..

so the new string looks like:

'Sommers<br>RE: The new offer<br>Please find attached....'

I thought I could explode the string based on the <span> tags but that opens up a lot of problems as I need to keep the text intact and formated as it is. I just want to insert the data and leave everything else untouched.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Smith Smithy
  • 585
  • 6
  • 24
  • Please be more specific: How do you (want to) translate the given span to the Agents name "Sommers"? Where does this information come from? Why didn't you just print out the name right away? Are you able to modify the given Span-Output? – dognose Aug 29 '13 at 19:46
  • the la_lname comes from a list of last names in a database. The template text comes from the database and then we go through and make a copy for each name with the name inserted in the placeholder – Smith Smithy Aug 29 '13 at 19:54
  • Possible duplciate of: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – hakre Aug 29 '13 at 20:05

3 Answers3

0

You could try preg_replace or use a DOM Parser, which is far more useful for navigating HTML-like-structure.

I should add that while regular expressions should work just fine in this example, you may need to do more complex things in the future or traverse more intrincate DOM structures for your replacements, so a DOM Parser is the way to go in this case.

alxgb
  • 543
  • 8
  • 18
0

To get what's between two parts of a string for example if you have

<span>SomeText</span>

If you want to get SomeText then I suggest using a function that gets whatever is between two parts that you put as parameters

<?php
function getbetween($content,$start,$end) {
$r = explode($start, $content);
if (isset($r[1])){
    $r = explode($end, $r[1]);
    return $r[0];
}
return '';
} 

$text = '<span>SomeText</span>';
$start = '<span>';
$end = '</span>';
$required_text = getbetween($text,$start,$end);
$full_line = $start.$required_text.$end;
$text = str_replace($full_line, 'WHAT TO REPLACE IT WITH HERE',$text);
Ali
  • 3,479
  • 4
  • 16
  • 31
  • ok, i see how this works but how do i find the placeholders – Smith Smithy Aug 29 '13 at 19:56
  • You need to add something unique to it.. for example make it .. only the start has to be unique.. the end will be the closest one to the start. – Ali Aug 29 '13 at 19:58
  • ok i can make that work... how do i replace the entire line INCLUDING the span? ... becomes sommers – Smith Smithy Aug 29 '13 at 20:03
  • I edited my answer to include that so you can replace the entire line including the spans and what's between them – Ali Aug 29 '13 at 20:07
0

Using PHP Simple HTML DOM Parser

$html = str_get_html('<span data-field="la_lname" data-table="user_properties">{Listing Agent Last Name}</span><br>RE: The new offer<br>Please find attached....');
$html->find('span')->innerText = 'New value of span';
Andrii Mishchenko
  • 2,626
  • 21
  • 19