查看: 1136|回复: 19
|
已部分解决: 转换静态网页的variable (PHP)
[复制链接]
|
|
狼儿好久没有回来了,最近在研究PHP产生静态网页的制作方法的时候,发现一个比较难解决的问题,突然想起以前很常光临的CARI,于是就来这里求救了。
狼儿是参考PHP Smarty以及BBS上的UBB系统的做法,希望可以在静态的网页上增加动态的link。
实际的流程是这样的:用户可以自己编写一段内容(比如说日记好了),编写以后的内容会被程式(Javascript)处理并且找出其中的关键字(通常是出现次数最多的名词)。找到以后,程式会在用户提交的时候(onSubmit)自动为其关键字加上UBB的编码(类似BBS上的code)。提交后的内容会被PHP处理,把UBB的编码(比如说:[link] CARI [/link])转换成HTML编码(<a href="{$link}"> CARI </link>),然后把这个内容储存成一个静态的HTML档案(比如说:about_cari.html)。然后当别人要从首页上看这个内容的时候(比如说:index.php?from=about_cari.html),程式就会自动从最新的数据库里面找到该关键字的相关连接,并且把他们放到页面里面去。这时候,被显示出来的内容里面所有的CARI的字眼就是一个连接,而且每一个连接都会去到不一样的网页(比如说:第一个CARI的字眼是连接到www.cari.com,第二个可能是forum.cari.com等等..)。
举例:
这个是用户在编写的内容
- LISP有很多种方言,各个实现中的语言不完全一样。1980年代Guy L. Steele编写了Common Lisp试图进行标准化,这个标准被大多数解释器和编译器所接受。在Unix/Linux系统中,还有一种和Emacs一起的Emacs Lisp(而Emacs正是用Lisp编写的)非常流行,并建立了自己的标准。
复制代码
当用户提交内容之前,程式(这一部分是Javascript的程式)会根据用户的内容以及所设定的关键字,直接把主要的关键字以UBB code的方式编写出来
经过Javascript处理以后的内容(假设关键字为LISP)
- [link]LISP[link]有很多种方言,各个实现中的语言不完全一样。1980年代Guy L. Steele编写了Common [link]Lisp[/link]试图进行标准化,这个标准被大多数解释器和编译器所接受。在Unix/Linux系统中,还有一种和Emacs一起的Emacs [link]Lisp[/link](而Emacs正是用[link]Lisp[/link]编写的)非常流行,并建立了自己的标准。
复制代码
当用户提交的时候,PHP就会以处理UBB的方式为乃内容加以处理(或则时使用str_replace()的方法,把其中的的[link]转换为<a href="{$link}">)。然后把相关的页面储存成静态的HTML档案
提交以后经过PHP处理的内容(假设关键字为LISP)
- <a href="{$link}">LISP</a>有很多种方言,各个实现中的语言不完全一样。1980年代Guy L. Steele编写了Common <a href="{$link}">Lisp</a>试图进行标准化,这个标准被大多数解释器和编译器所接受。在Unix/Linux系统中,还有一种和Emacs一起的Emacs <a href="{$link}">Lisp</a>(而Emacs正是用<a href="{$link}">Lisp</a>编写的)非常流行,并建立了自己的标准。
复制代码
我这样做的目的是为了说当这个页面被显示的时候(经过Smarty来显示),电脑会自动从我设定的数据库中找出相关的连接,然后就会把这几个连接根据他的名气分配在用户所写的内容里面的link。名气越高的会出现在越前面以及出现的次数也会越多。
比如说找到的相关连接为
- www.list.com - 名气 6
- www.more_lisp.com - 名气 8
- www.more_and_more_lisp.com - 名气 3
复制代码
经过PHP + Smarty 再次处理的内容
- <a href="www.more_lisp.com">LISP</a>有很多种方言,各个实现中的语言不完全一样。1980年代Guy L. Steele编写了Common <a href="www.list.com">Lisp</a>试图进行标准化,这个标准被大多数解释器和编译器所接受。在Unix/Linux系统中,还有一种和Emacs一起的Emacs <a href="www.more_and_more_lisp.com">Lisp</a>(而Emacs正是用<a href="www.more_lisp.com">Lisp</a>编写的)非常流行,并建立了自己的标准。
复制代码
现在重点来了,狼儿想要请问一下最后一个部分(根据名气分配link)要怎么解决。如果继续使用str_replace()的话,全部的link都会指向同一个地方,这并不是狼儿要的。还有,狼儿不想直接把有link的部分储存成静态网页是因为数据库里面的link每天会更新,每次被显示出来的link会不一样。
[ 本帖最后由 JR86 于 10-12-2006 08:30 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 10-12-2006 01:20 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 10-12-2006 01:29 AM
|
显示全部楼层
你的名气分配... 要跟着什么条件?
如果一个关键词在一篇文章出现三次, 理论上要怎么分配? |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 01:39 AM
|
显示全部楼层
原帖由 exiang 于 10-12-2006 01:20 AM 发表
你的做法很奇怪,我是这么想的。
你想要做auto link 吧。
为什么不干脆让php做完全部东西?
不然,便完全做javascript 方式, 如http://www.vibrantmedia.com/advertisers/intellitxt.asp 的 intellitext
我的做法跟你所提供的网站很相识,可是差别就在于说,同样的文字,可以指向不同的网站,同样是"CARI"的字眼,在第一段里面可能使指向cari.com,第二段可能是chinese.cari.com之类的。
而且(我觉得)难度最高的是,原作者可以事后为这一些关键字设定他要的连接。比如说,它可以指定第一个出现的CARI指向chinese.cari.com,第二个出现的指向cari.com这样。
我本来打算使用preg_replace()的,可是这个指令会把整个内容里面的字眼都转向同一个link。
我的失败举例:
- <?php
- $string = 'A news article is an article published in a print or Internet news medium such as a newspaper, newsletter, news magazine, or news-oriented website that discusses current or recent news of either general interest (i.e. daily newspapers) or on a specific topic (i.e. political or trade news magazines, club newsletters, or technology news websites).';
- $patterns = '/news/';
- $replacements = '<a href="{$link}">news</a>';
- echo preg_replace($patterns, $replacements, $string);
- ?>
复制代码
[ 本帖最后由 JR86 于 10-12-2006 01:59 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 01:59 AM
|
显示全部楼层
原帖由 goatstudio 于 10-12-2006 01:29 AM 发表
你的名气分配... 要跟着什么条件?
如果一个关键词在一篇文章出现三次, 理论上要怎么分配?
如果只出现三次,可是数据库里面有10个的话,那么就只选取名气最高的3个link,然后随机分配。
或则是用户可以指定他要显示的连接。
|
|
|
|
|
|
|
|
发表于 10-12-2006 02:09 AM
|
显示全部楼层
原帖由 JR86 于 10-12-2006 01:59 AM 发表
如果只出现三次,可是数据库里面有10个的话,那么就只选取名气最高的3个link,然后随机分配。
或则是用户可以指定他要显示的连接。
如果只是那样而已... 你可以从资料库选出名气最高的三个... 然后用 php 来做随机分配... 不就是这样吗? 还是我理解错了?  |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 03:14 AM
|
显示全部楼层
原帖由 goatstudio 于 10-12-2006 02:09 AM 发表
如果只是那样而已... 你可以从资料库选出名气最高的三个... 然后用 php 来做随机分配... 不就是这样吗? 还是我理解错了?
基本的理论是没有错,可是我要用什么function去分配呢?
我已经把用户编写的内容转换成这样了
- Ina Garten is an <a href="{$link}" title="{$title}">American</a> chef, former caterer, cookbook author, columnist, and hostess of the Food Network program Barefoot Contessa. Known for demystifying fine cuisine with an emphasis on quality ingredients and timesaving tips, she has been championed by the likes of Martha Stewart, Oprah Winfrey, and Patricia Wells as a top authority on cooking and home entertaining. Garten had no formal training, and instead taught herself culinary techniques with the aid of <a href="{$link}" title="{$title}">French</a> and <a href="{$link}" title="{$title}">New England</a> cookbooks. Later, she relied on intuition and feedback from customers and friends to refine her recipes. She was mentored chiefly by Eli Zabar, of Eli's Manhattan and Eli's Breads fame, and domestic maven Stewart. Among her hallmark dishes are cœur à la crème, celery root remoulade, pear clafouti, and a simplified version of bœuf bourguignon. Her culinary fame began with her gourmet food store, Barefoot Contessa; Garten parlayed this success into a string of best-selling cookbooks, magazine columns, self-branded convenience foods, and a popular Food Network television show.
复制代码
接下来我应该怎么编写分配的function呢?有什么function / class可以让我把里面的$link转换成link_1,link_2,link_3之类的呢?
|
|
|
|
|
|
|
|
发表于 10-12-2006 09:28 AM
|
显示全部楼层
用 php rand() function
$randomSelect= rand(0, 2);
@mysql_query (SELECT * FROM tblrandom ORDER BY random DESC LIMIT $randomSelect, 1)
可是这么做也一样 random 一样的东西
所以。。goatstudio 的方法就可以了
选出了 1 record 个然后在 function 做一定不是问题了
<?
function autogenerate(){
$a=rand(0,2);
return $a;
}
echo autogenerate().'<BR>';
echo autogenerate().'<BR>';
?>
这并不完整,logic 我已经给你了哦。。。。要怎么做就靠你了,我只是大概大概
嘻嘻,看你自己安排了。。。
[ 本帖最后由 cupid25 于 10-12-2006 10:19 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 01:53 PM
|
显示全部楼层
原帖由 cupid25 于 10-12-2006 09:28 AM 发表
用 php rand() function
$randomSelect= rand(0, 2);
@mysql_query (SELECT * FROM tblrandom ORDER BY random DESC LIMIT $randomSelect, 1)
可是这么做也一样 random 一样的东西
所以。。goat ...
资料抽取的部分狼儿已经解决了,可是现在的问题就是在于怎么把原本很统一的variable名称,代替成个别的variable名称。
daily.html
- Ina Garten is an <a href="{$link}" title="{$title}">American</a> chef, former caterer, cookbook author, columnist, and hostess of the Food Network program Barefoot Contessa. Known for demystifying fine cuisine with an emphasis on quality ingredients and timesaving tips, she has been championed by the likes of Martha Stewart, Oprah Winfrey, and Patricia Wells as a top authority on cooking and home entertaining. Garten had no formal training, and instead taught herself culinary techniques with the aid of <a href="{$link}" title="{$title}">French</a> and <a href="{$link}" title="{$title}">New England</a> cookbooks. Later, she relied on intuition and feedback from customers and friends to refine her recipes. She was mentored chiefly by Eli Zabar, of Eli's Manhattan and Eli's Breads fame, and domestic maven Stewart. Among her hallmark dishes are cœur à la crème, celery root remoulade, pear clafouti, and a simplified version of bœuf bourguignon. Her culinary fame began with her gourmet food store, Barefoot Contessa; Garten parlayed this success into a string of best-selling cookbooks, magazine columns, self-branded convenience foods, and a popular Food Network television show.
复制代码
以上的文章是经过UBB转换的,里面的Smarty的variable都是同一个名字。如果狼儿直接显示出来的话,全部的variable都会被assign同一个link。
狼儿现在想要把全部的{$link}代替成{$link[0]},{$link[1]},{$link[2]}之类的。这样再assign link的时候会比较方便。可是问题又回来了。狼儿要怎么替换这一些variable的名称呢?
|
|
|
|
|
|
|
|
发表于 10-12-2006 02:33 PM
|
显示全部楼层
原帖由 JR86 于 10-12-2006 01:53 PM 发表
资料抽取的部分狼儿已经解决了,可是现在的问题就是在于怎么把原本很统一的variable名称,代替成个别的variable名称。
daily.html
Ina Garten is an <a href="{$link}" title="{$tit ...
我有点不明白你说的
<a href="{$link}" title="{$title}">American</a>
<a href="{$link}" title="{$title}">New England</a>
<a href="{$link}" title="{$title}">French</a>
这个 三个link 是否属于原本都有自己的一个 link? america 有 america 的 link ? new england 有 england 的 link?
还是这个三个 link 都是 random ?
但你必须说清楚。。。你的 link variable 有什么
而且到底是 random 还是不是?
必须说清楚和 complete set example 那个文章帮不了的,因为要说你想要的。。。东西是哪里来哪里去
因为我不成用过 smarty 这样东西。。。除了 hardcode php
呵呵
其实都可以做(应该),你就是要写 multi array 也就是把你 call 出来的 record set 然后收在 array 里 |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 04:02 PM
|
显示全部楼层
原帖由 cupid25 于 10-12-2006 02:33 PM 发表
我有点不明白你说的
<a href="{$link}" title="{$title}">American</a>
<a href="{$link}" title="{$title}">New England</a>
<a hr ...
我自己写了一个很粗糙的function,可以请问一下还有什么比较完整的class / function可以做到这样的效果的吗?
- $new_id = 1; //新article的id
- $text = 'Ina Garten is an <a href="{$link}" title="{$title}">American</a> chef, former caterer, cookbook author, columnist, and hostess of the Food Network program Barefoot Contessa. Known for demystifying fine cuisine with an emphasis on quality ingredients and timesaving tips, she has been championed by the likes of Martha Stewart, Oprah Winfrey, and Patricia Wells as a top authority on cooking and home entertaining. Garten had no formal training, and instead taught herself culinary techniques with the aid of <a href="{$link}" title="{$title}">French</a> and <a href="{$link}" title="{$title}">New England</a> cookbooks. Later, she relied on intuition and feedback from customers and friends to refine her recipes. She was mentored chiefly by Eli Zabar, of Eli\'s Manhattan and Eli\'s Breads fame, and domestic maven Stewart. Among her hallmark dishes are cœur à la crème, celery root remoulade, pear clafouti, and a simplified version of bœuf bourguignon. Her culinary fame began with her gourmet food store, Barefoot Contessa; Garten parlayed this success into a string of best-selling cookbooks, magazine columns, self-branded convenience foods, and a popular Food Network television show.'; //这一些都是经过UBB转换的内容
- $link_num = 3; //数据库里面有的连接数量,这是假设的狼儿设定为3是因为以上的内容只有3个空间,这样做是为了避免程式出错
- function parseLink($text, $link_num)
- {
- $start = 0;
- for($i=0; $i<$link_num; $i++)
- {
- $link = '{$link}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'linkurl\']}', $start, strlen($link));
- }
- $start = 0;
- for($i=0; $i<($link_num); $i++)
- {
- $link = '{$title}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'title\']}', $start, strlen($link));
- }
- return $text;
- }
- echo parseLink($text, $link_num);
复制代码
以上code是狼儿花了几个小时时间慢慢看PHP manual写出来的(很笨是吧?),不知道PHP里面还有没有一些类似的function呢?
|
|
|
|
|
|
|
|
发表于 10-12-2006 04:39 PM
|
显示全部楼层
原帖由 JR86 于 10-12-2006 04:02 PM 发表
我自己写了一个很粗糙的function,可以请问一下还有什么比较完整的class / function可以做到这样的效果的吗?
$new_id = 1; //新article的id
$text = 'Ina Garten is an <a href="{$lin ...
终于明白你想要的了,哈哈
原来就是 forum 那样,以为是。。。查字典那种。。。自动找 similar word 然后放 link
都 ok 了。。。至少自己能弄得出
没有任何意见,看看别人咯。。。哈哈
可是你只有做到 link tag 变成 aherf 吧?可以研究如果做多多那种。。。
这样很好嘛自己找 function ,我也是那样呢。。。需要的时候才找,我哦也是 0 开始。。完全不会 php
反而我忽然被叫做 php 什么都不会都是 php.net 哪儿慢慢看。。现在算习惯了
[ 本帖最后由 cupid25 于 10-12-2006 04:43 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 04:52 PM
|
显示全部楼层
原帖由 cupid25 于 10-12-2006 04:39 PM 发表
终于明白你想要的了,哈哈
原来就是 forum 那样,以为是。。。查字典那种。。。自动找 similar word 然后放 link
都 ok 了。。。至少自己能弄得出
没有任何意见,看看别人咯。。。哈哈
可是你只有做到 ...
可是狼儿在想说PHP应该有内建的一些fuction可以做到这一点吧?狼儿现在的做法是很“土法炼钢”的。这样的写法完全没有OPP的观念。(感觉狼儿这样写很浪费资源) =_=
|
|
|
|
|
|
|
|
发表于 10-12-2006 05:53 PM
|
显示全部楼层
原帖由 JR86 于 10-12-2006 04:52 PM 发表
可是狼儿在想说PHP应该有内建的一些fuction可以做到这一点吧?狼儿现在的做法是很“土法炼钢”的。这样的写法完全没有OPP的观念。(感觉狼儿这样写很浪费资源) =_=
要完全用 OOP... 你需要重新 define 过你的 class 和设计方法...
PHP 的 OOP 和 J2EE / .Net 平台有点不一样.
另外... 基本上还是不太了解你的问题... 我以为用 php 来处理随机就可以了.
但是, 你的问题解决了吗? |
|
|
|
|
|
|
|

楼主 |
发表于 10-12-2006 08:10 PM
|
显示全部楼层
原帖由 goatstudio 于 10-12-2006 05:53 PM 发表
要完全用 OOP... 你需要重新 define 过你的 class 和设计方法...
PHP 的 OOP 和 J2EE / .Net 平台有点不一样.
另外... 基本上还是不太了解你的问题... 我以为用 php 来处理随机就可以了.
但是, 你的问题 ...
感谢羊兄啊!(狼跟羊道谢?)哈哈哈!
基本上狼儿的问题算是解决了一部分。其余的部分,狼儿会很努力的继续研究的。
如果还是不明白的话,狼儿会很不客气地继续烦羊兄的。

|
|
|
|
|
|
|
|
发表于 10-12-2006 11:40 PM
|
显示全部楼层
原帖由 JR86 于 10-12-2006 01:53 PM 发表
资料抽取的部分狼儿已经解决了,可是现在的问题就是在于怎么把原本很统一的variable名称,代替成个别的variable名称。
daily.html
Ina Garten is an <a href="{$link}" title="{$tit ...
不是很明白你的表达。
你是不是要把变数变成变数呢?这很简单。 |
|
|
|
|
|
|
|
发表于 11-12-2006 06:06 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 11-12-2006 06:06 PM
|
显示全部楼层
原帖由 blue333 于 10-12-2006 11:40 PM 发表
不是很明白你的表达。
你是不是要把变数变成变数呢?这很简单。
真不好意思,狼儿的表达能力不怎么好,害各位看的一头雾水。
其实狼儿要的东西很简单的,主要是更改variable的名称。
把全部{$link}更改为{$link[0]},{$link[1]},{$link[2]}之类的。
你执行一下下面的PHP就知道狼儿要表达的东西了。
(程式很粗糙,请多多包含。) 
- $new_id = 1; //新article的id
- $text = 'Ina Garten is an <a href="{$link}" title="{$title}">American</a> chef, former caterer, cookbook author, columnist, and hostess of the Food Network program Barefoot Contessa. Known for demystifying fine cuisine with an emphasis on quality ingredients and timesaving tips, she has been championed by the likes of Martha Stewart, Oprah Winfrey, and Patricia Wells as a top authority on cooking and home entertaining. Garten had no formal training, and instead taught herself culinary techniques with the aid of <a href="{$link}" title="{$title}">French</a> and <a href="{$link}" title="{$title}">New England</a> cookbooks. Later, she relied on intuition and feedback from customers and friends to refine her recipes. She was mentored chiefly by Eli Zabar, of Eli\'s Manhattan and Eli\'s Breads fame, and domestic maven Stewart. Among her hallmark dishes are cœur à la crème, celery root remoulade, pear clafouti, and a simplified version of bœuf bourguignon. Her culinary fame began with her gourmet food store, Barefoot Contessa; Garten parlayed this success into a string of best-selling cookbooks, magazine columns, self-branded convenience foods, and a popular Food Network television show.'; //这一些都是经过UBB转换的内容
- $link_num = 3; //数据库里面有的连接数量,这是假设的狼儿设定为3是因为以上的内容只有3个空间,这样做是为了避免程式出错
- function parseLink($text, $link_num)
- {
- $start = 0;
- for($i=0; $i<$link_num; $i++)
- {
- $link = '{$link}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'linkurl\']}', $start, strlen($link));
- }
- $start = 0;
- for($i=0; $i<($link_num); $i++)
- {
- $link = '{$title}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'title\']}', $start, strlen($link));
- }
- return $text;
- }
- echo parseLink($text, $link_num);
复制代码
这个被处理以后的内容会被储存为一个纯HTML档案,然后当游览者要看的时候,PHP Smarty就会parse这个页面,然后显示出来。
|
|
|
|
|
|
|
|
发表于 11-12-2006 06:19 PM
|
显示全部楼层
原来如此,那么你hardcode $link_num 的value 不是会有问题咯? |
|
|
|
|
|
|
|

楼主 |
发表于 11-12-2006 06:41 PM
|
显示全部楼层
原帖由 exiang 于 11-12-2006 06:19 PM 发表
原来如此,那么你hardcode $link_num 的value 不是会有问题咯?
就目前为止,还没有出现什么很大的问题。因为狼儿后来发现这样做其实不太对,所以狼儿已经为这个程式作了一点点的修改。
- function getWordsNum($word, $text)
- {
- $text = preg_split("/[\s,.:;"]+/", $text);
- $num = array_count_values($text);
- @$num = $num[$word];
- if($num==null) $num = 0;
- return $num;
- }
- function parseLink($text, $word_num)
- {
- $start = 0;
- for($i=0; $i<$word_num; $i++)
- {
- $link = '{$link}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'linkurl\']}', $start, strlen($link));
- }
- $start = 0;
- for($i=0; $i<$word_num; $i++)
- {
- $link = '{$title}';
- $start = strpos($text, $link, ($start+$i));
- $text = substr_replace($text, '{$link['.$i.'][\'title\']}', $start, strlen($link));
- }
- return $text;
- }
- $content = 'Ina Garten is an <a href="{$link}" title="{$title}">American</a> chef, former caterer, cookbook author, columnist, and hostess of the Food Network program Barefoot Contessa. Known for demystifying fine cuisine with an emphasis on quality ingredients and timesaving tips, she has been championed by the likes of Martha Stewart, Oprah Winfrey, and Patricia Wells as a top authority on cooking and home entertaining. Garten had no formal training, and instead taught herself culinary techniques with the aid of <a href="{$link}" title="{$title}">French</a> and <a href="{$link}" title="{$title}">New England</a> cookbooks.';
- $word_num = getWordsNum('{$link}', $content); // 得到内容里面{$link}的数量
- $content = parseLink($content, $word_num); // 把{$link}转换为{$link[0]},{$link[1]}之类的。
- echo $content; // 这个就是会被储存为纯HTML档案的内容
复制代码
这样写得话,被储存成纯HTML档案的文件就不会出错了,然后狼儿会写一个程式去从数据库中抽取相同数量的link,如果数据库里面的link储量比页面的来得少的话,程式就会自动产生一些link来代替(基本上都是自己网站的一些link)。
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|