【问题标题】:Coldfusion while uploading replacing images path REGEXColdfusion同时上传替换图像路径REGEX
【发布时间】:2016-04-20 05:27:06
【问题描述】:

我正在尝试编写正则表达式来搜索和替换图像路径,并保持与我的远程服务器路径相同的文件名现有路径。

用户正在上传我想要的 HTML 文件,同时上传替换路径,然后保存到数据库中。

我写了一些正则表达式,我在互联网上找到的正则表达式似乎不起作用。

请在下面找到代码 sn-p...

for example this my HTML content.

<cfsavecontent variable="htmlcont">

<html>
<head>
</head>
<body>
<a href='http://www.w3schools.com'><img src='http://www.w3schools.com/images/w3schools.png' alt='W3Schools.com' class='img-responsive'></a>
<div class="w3-row">
<div class="w3-third w3-center">
<h2>JPG Images</h2>
<img alt="Mountain View" src="pic_mountain.jpg" style="width: 304px; height: auto" class="img-responsive">
</div>
<div class="w3-third w3-center">
<h2>GIF Images</h2>
<img alt="" src="html5.gif" style="width: 128px; height: auto" class="img-responsive">
</div>
<div class="w3-third w3-center">
<h2>PNG Images</h2>
<img alt="Graph" src="pic_graph.png" style="width: 170px; height: auto" class="img-responsive">
</div>
</div>

<table class="lamp"><tr>
<th style="width:34px">
<img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"></th>
<td>Always specify the width and height of an image. If width and height are not specified, the page will flicker while the image loads.
</td>
</tr></table>
<hr>

<table class="lamp"><tr>
<th style="width:34px">
<img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"></th>
<td>Add &quot;border:0;&quot; to prevent IE9 (and earlier) from displaying a border around the image.</td>
</tr></table>
<hr>
</body>
</html>
</cfsavecontent>

我正在尝试替换

http://www.w3schools.com/images/w3schools.png

http://myremoteServer.com/234001/images/w3schools.png

<img alt="Graph" src="pic_graph.png" style="width: 170px; height: auto" class="img-responsive">

<img alt="Graph" src="http://myremoteServer.com/234001/images/pic_graph.png" style="width: 170px; height: auto" class="img-responsive">

代码:

<cfset regxv =  'src="\K[^"]*(?=")' />
<cfset resluthtml = REReplace (htmlcont,regxv, "http://myremoteServer.com/234001/images/") />


<cfdump var="#resluthtml#" label="resluthtml" >

【问题讨论】:

标签: regex coldfusion coldfusion-9 cfml railo


【解决方案1】:

使用 ColdFusion 的内置引擎(Jakarta ORO、Perl)进行捕获是一件非常痛苦的事情。所以让我们在这里使用一些 Java 魔法(POSIX):

<cfset regex    = createObject("java", "java.util.regex.Pattern").compile('<img [^>]*src=["'']([^"'']*)["'']')>

<cfset result   = createObject("java", "java.lang.StringBuilder").init()>
<cfset matcher  = regex.matcher(htmlcont)>
<cfset last     = 0>

<cfloop condition="matcher.find()">

    <cfset result.append(
        htmlcont.substring(
            last,
            matcher.start()
        )
    )>

    <cfset token = matcher.group(
        javaCast("int", ( matcher.groupCount() gte 1 ? 1 : 0 ))
    )>

    <!--- go with your replace logic here, token is the value of the [src] attribute --->
    <cfset token = ("http://myremoteServer.com/234001/images/" & listLast(token, "/"))>

    <cfset result.append(token)>

    <cfset last = matcher.end()>
</cfloop>

<cfset result.append(
    htmlcont.substring(last)
)>
<cfset result = result.toString()>

<cfdump var="#result#">

【讨论】:

  • &lt;cfsavecontent variable="htmlcont"&gt; &lt;img alt="Mountain View" src="pic_mountain.jpg" style="width: 304px; height: auto" class="img-responsive"&gt; &lt;img alt="" src="html5.gif" style="width: 128px; height: auto" class="img-responsive"&gt; &lt;img alt="Graph" src="pic_graph.png" style="width: 170px; height: auto" class="img-responsive"&gt; &lt;img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"&gt;&lt;/th&gt; &lt;img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"&gt;&lt;/th&gt; &lt;/cfsavecontent&gt;`
  • 输出: myremoteServer.com/234001/images/pic_mountain.jpg' style="width: 304px; height: auto" class="img-responsive"> myremoteServer.com/234001/images/html5.gif' style="width: 128px; height: auto" class="img-responsive"> myremoteServer.com/234001/images/pic_graph.png' style="width: 170px; height: auto" class="img -responsive"> myremoteServer.com/234001/images//images/lamp.jpg' alt="Note" style="height:32px;width:32px"> `
  • 有没有我只能得到图像文件名??它返回令牌“image/image.jpg”
  • 使用listLast(token, "/") 这样做。我相应地编辑了我的答案。
  • &lt;cfset token = ("&lt;img src='http://myremoteServer.com/234001/images/" &amp; ListLast(listLast(token, "/")) &amp; "'")&gt;
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多