我正在使用<cfhttp>从另一个网站(coldfusion)和resolveurl="true"中拉入内容,这样所有的链接都可以工作。我的问题是resolveurl正在使锚链接(href="#search")绝对链接以及破坏他们。我的问题是,有没有办法让resolveurl="true"绕过锚链接?
发布于 2019-10-10 03:58:08
首先,让我们使用评论中发布的来自Adobe.com的教程代码。你会想要做一些类似的事情。
<cfhttp url="https://www.adobe.com"
method="get" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfscript>
// Find all the URLs in a web page retrieved via cfhttp
// The search is case sensitive
result = REMatch("https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?", httpResp.Filecontent);
</cfscript>
<!-- Now, Loop through those URLs--->
<cfoutput>
<cfloop array="#result#" item="item" index="index">
<cfif LEFT(item, 1) is "##">
<!---Your logic if it's just an anchor--->
<cfelse>
<!---Your logic if it's a full link--->
</cfif>
<br/>
</cfloop>
</cfoutput>如果它试图像你说的那样在锚点之前返回一个完整的网址,(我用resolveurl="true"得到了不一致的结果),用这个来点击它,只获取你想要的部分。
<cfoutput>
<cfloop array="#result#" item="item" index="index">
#ListLast(item, "##")#
</cfloop>
</cfoutput>这段代码所做的就是获取所有的URL,并解析它们的锚点。
你必须在你的循环中决定下一步要做什么。也许可以保留这些值并将它们添加到一个新的数组中,这样您就可以将其保存到链接固定的地方?
在这种情况下是不可能假设的。
发布于 2019-10-10 21:26:38
似乎没有一种方法可以阻止CF解析散列。在我们使用它时,当前的结果实际上是有益的,因为当我们呈现来自另一个站点的内容时,我们通常希望用户被发送到那里。
这是一种使用正则表达式将链接href值替换为仅锚点的方法。我敢肯定,如果html格式真的不正确,这里可能会出现一系列问题。
<cfsavecontent variable="testcontent">
<strong>test</strong>
<a href="http://google.com">go to google</a>
<a href="http://current.domain/thispage#section">go to section</a>
</cfsavecontent>
<cfset domain = replace("current.domain", ".", "\.", "all") />
<cfset match = "(href\s*=\s*(""|'))\s*(http://#domain#[^##'""]+)(##[^##'""]+)\s*(""|')" />
<cfset result = reReplaceNoCase(testcontent, match, "\1\4\6", "all") />
<cfoutput><pre>#encodeForHTML(result)#</pre></cfoutput>输出
<strong>test</strong>
<a href="http://google.com">go to google</a>
<a href="#section>go to section</a>如果您使用js/jquery在普通页面中显示内容,另一种选择是遍历显示的每个链接,并将其更新为锚点。这将不太可能是格式错误的html的错误。如果你对这种方法感兴趣,请告诉我。
https://stackoverflow.com/questions/58309170
复制相似问题