473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Replace all links with url index?

Hello,

I'm writing a newsletter poster for a website. I have some
html news items which I want to turn into plain text and prettify
before emailing it.

Now, i have found wordwrap and strip_tags so the only problem
remaining is the links. Below you see what I like to see, but
I haven't got a clue how to get there.

$html = '<p>My <a href="http://go.there/stuff">link somewhere</a>.</p>';
doSomeMagic($html);
echo $html;

After doSomeMagic I'd like the echo to look like this:

My link somewhere[1].
[1] http://go.there/stuff

That is, in the text I want an reference, and below the entire paragraph
i want a link index.

I'd be grateful for any hints how to get there.
--
Fredrik Jonson
Jul 16 '05 #1
2 8402
Fredrik Jonson <pt*****@student.bth.se> wrote:
I'm writing a newsletter poster for a website. I have some
html news items which I want to turn into plain text and prettify
before emailing it.

Now, i have found wordwrap and strip_tags so the only problem
remaining is the links. Below you see what I like to see, but
I haven't got a clue how to get there.

$html = '<p>My <a href="http://go.there/stuff">link somewhere</a>. </p>'; doSomeMagic($html);
echo $html;


Hi Fredrik,

I wrote something a while ago that does that:

<?php
$lcount=0;
$globMatches;

function increase(){
global $lcount;
$lcount++;
return $lcount;
}

function stripLinks($origtext){
global $globMatches;
$filteredtext = "";
$pattern = "/(<a href=\")(.+?)(\")(\s*)(>)(.+?)(<\/a>)/e";
$replace = "'\\6['.increase().']'";
preg_match_all($pattern, $origtext, $matches);
$globMatches[] = $matches;
$filteredtext = preg_replace($pattern, $replace, $origtext);
return $filteredtext;
}

function makeLinkFooter(){
global $globMatches, $lcount;
$appendix = "";
$m=1;
for($j=0;$j<$lcount;$j++){
for($i=0;$i<count($globMatches[$j][2]);$i++){
$appendix .= "[".$m."] ".$globMatches[$j][2][$i]."<br>\n";
$m++;
}
}
return $appendix;
}

$html = 'My <a href="http://go.there/stuff">link somewhere</a>.';
$html_new = stripLinks($html);
$footer = makeLinkFooter();

echo $html_new."\n\n";
echo $footer."\n";
?>

Hope this helps;
JOn
Jul 16 '05 #2
In <Xn**************************@130.133.1.4> Jon Kraft wrote:
I wrote something a while ago that does that:
[snip - some code]
Hope this helps; JOn


Yes, thanks. I had already started working with some code using
preg_replace_callback, which I of course stumbled on _right_ after
I posted my first message. But I had problems with the regex, which
with some hints from your code I got to work.

<?php
$current_link = 0;
$link_index;

function link_indexer($matches)
{
global $link_index;
global $current_link;
// save the match in the global index
$link_index[$current_link] = $matches[1];
$current_link++;
// return the "plain" text with a reference.
return $matches[2] . '[' . $current_link . ']';
}

$html = '<p>My <a href="http://go.there/stuff">link somewhere</a>.</p>';

$text = preg_replace_callback(
'/<A href=\"(.+?)\">(.+?)<\/A>/i',
'link_indexer',
$html);
// remove other html tags
strip_tags($text);

// print the plain version
echo $text; // renders: "My link somewhere[0]."

// print the index
$i = 0;
foreach ($link_index as $link) {
print "[$i] $link\n";
$i++;
}
// renders: "[0] http://go.there/stuff"
?>

--
Fredrik Jonson
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
1
by: Kerberos | last post by:
I found an interesting "menu", I don't know how you call it. It displays a link for 2 seconds, then it displays another link for 2 seconds, and so on. Here it is: http://www.alibaba.com under...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
3
by: Brad | last post by:
I see the use of Javascript replace all over the web. What are all the character sequences? (sorry I am a bit of a newbie at this). i.value.replace(/+/g, ''); I understand that /g is global...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
3
by: laredotornado | last post by:
Hi, I'm trying to replace all non-numeric characters with nothing in my variable. However, this doesn't seem to work ... var myMixedVar = myMixedVar.replace(/^/, ""); Any suggestions what...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.