?
Solved

fix URL titles

Posted on 2007-04-02
8
Medium Priority
?
256 Views
Last Modified: 2007-04-02
what im doing is creating a script that allows users to create HREFs.  they type in a string, select some word(s) and hit a button and my script inserts <a href="" and title=""></a> wrapped around selected word(s).

example - my string looks like this:

<p>this is a link to <a title="google" href="http://www.google.co.uk">google</a> and this is a link to <a title="google" href="http://www.quest-recruiting.com">quest</a></p>

as you can see, my second url title is google again.  i got help from hernst42 with extracting all the href title values from a string and i have these stored in an array called $titles.

so what i did next was this:

foreach($titles[1] as $title)
{
      $data = str_replace('title=""','title="'.$title.'"',$data);
}

but it isnt correct either.  how would you suggest i fix this?  basically i want to get the value between all the <a></a>'s in the string and insert that value into the correct <a title="" href=""></a>...
0
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 18835454
If you go through all titles in the same sequence use
$data = str_replace('title=""','title="'.htmlentities(strip_tags($title)).'"',$data,1);
so only the first occurence of the  title="" is replaced and not all. and for the second match the second found title="" is replaces, ....
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18835475
ah ok...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18835956
if i add the 1 to the end, my PHP wont run...

im running PHP 5.4.1
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 18836209
ok I've missunderstood the docu there. The count must be a variable which returns the numer of replacements. So try
$data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
which has a limit argument
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836834
have you got an example working as i still cant get mine working:

$data = 'this is <a title="" href="">link1</a> and this is <a title="" href="">link2</a>';

preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);

$count = 1;

foreach($titles[1] as $title)
{
      $data = str_replace('title=""','title="'.htmlentities(strip_tags($title)).'"',$data,$count);
}

echo $data;
?>
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18836904
This worked for me:
$data = 'this is <a title="" href="">link1</a> and this is <a title="" href="">link2</a>';
preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);
$count = 1;
foreach($titles[1] as $title)
{
    $data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,$count);
}
echo $data;

the proble is that str_replace does not have a limit option, but preg_replace has a limit option.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836928
sorry - i didnt see the preg_replace in your first solution...  my eyes skipped over it...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18836930
thank you BTW!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question