Coldfusion搜索HTML <textarea>然后添加全部&lt; img&gt;数组的源URL

时间:2017-09-08 12:40:01

标签: html loops coldfusion-11

&lt; p&gt;&lt; strong&gt;我需要帮助了解如何在找到第一个匹配项后继续循环。&lt; / strong&gt;&lt; / p&gt; &lt; p&gt; @luxdvie创建的循环很棒(&lt; a href =“https://stackoverflow.com/questions/46001839/coldfusion-search-html-textarea-then-covert-base64-string-to-file -on-server“&gt;链接在这里&lt; / a&gt;)。我正在尝试扩展该循环以查找所有图像文件名并将它们放入数组中。 我在trycf.com上测试它(&lt; a href =“https://trycf.com/gist/f8eef6a1cd2d0d39a6d0e4433405067a/lucee5?theme=monokai”rel =“nofollow noreferrer”&gt;链接这里&lt; / a&gt;)但是有时我们的互联网过滤器会阻止网站。&lt; / p&gt; &lt; p&gt;我认为完成此操作的逻辑是找到第一次出现的图像源,将其结束设置为新步骤的开始,并继续查找下一次出现,直到文本区域结束。 &LT; / p为H. &lt; p&gt;以下是我尝试执行此操作的代码:&lt; / p&gt; &lt; pre&gt;&lt; code&gt;&lt; cfset search_text_area =#form.overview_text#&gt; &lt; cfset length_of_text_field = #Len(search_text_area)#&gt; &lt; cfset image_url_header =“https:// ImagesFolder / subFolder /”&gt; &lt; cfset image_url_closure ='“'&gt; &lt; cfset image_extension_png =“。png”&gt; &lt; cfset image_list_array_on_submit = []&gt; &lt; cfset continue_searching_url = true&gt; &lt; cfset first_pass = true&gt; &lt; cfset go_thru_again = false&gt; &lt; cfset counter = 0&gt; &lt; cfloop condition =“continue_searching_url eq true”&gt; &LT; CFOUTPUT&GT;     &lt; cfset counter = counter + 1&gt;     &lt; cfif counter GTE 5&gt;         &lt; script&gt; console.log('警告!长循环:x'+&lt; cfoutput&gt;#counter#&lt; / cfoutput&gt;);&lt; / script&gt;         &LT; cfbreak&GT;     &LT; / CFIF&GT;     &lt; cfif starting_index_url eq 0&gt;         &lt; cfset continue_searching_url = false&gt;         &LT; cfcontinue&GT;     &LT; / CFIF&GT;     &lt; cfif first_pass eq true&gt;         &lt; cfset starting_index_url = find(image_url_header,search_text_area)&gt;         &lt; cfset ending_index_url = find(image_url_closure,search_text_area,starting_index_url)&gt;         &lt; cfset full_occurrence_url = mid(#search_text_area#,(#starting_index_url#),((#end_index_url#) - #starting_index_url#))&gt;         &lt; cfset local_occurrence_url = mid(#search_text_area#,(#starting_index_url#),((#end_index_url#) - #starting_index_url#))&gt;         &lt; cfset ArrayAppend(image_list_array_on_submit,(local_occurrence_url))&gt;         &lt; cfset first_pass = false&gt;         &lt; cfset go_thru_again = true&gt;         &lt; cfset starting_index_url =#ending_index_url#&gt;     &LT; / CFIF&GT;     &lt; cfif go_thru_again = true&gt;         &lt; cfif starting_index_url NEQ length_of_text_field&gt;             &lt; cfset starting_index_url = find(image_url_header,search_text_area)&gt;             &lt; cfset ending_index_url = find(image_url_closure,search_text_area,starting_index_url)&gt;             &lt; cfset full_occurrence_url = mid(#search_text_area#,(#starting_index_url#),((#end_index_url#) - #starting_index_url#))&gt;             &lt; cfset local_occurrence_url = mid(#search_text_area#,(#starting_index_url#),((#end_index_url#) - #starting_index_url#))&gt;             &lt; cfset ArrayAppend(image_list_array_on_submit,(local_occurrence_url))&gt;             &lt; cfset first_pass = false&gt;             &lt; cfset go_thru_again = true&gt;             &lt; cfset starting_index_url =#ending_index_url#&gt;         &LT; CFELSE&GT;             &lt; cfset starting_index_url = 0&gt;             &LT; cfcontinue&GT;         &LT; / CFIF&GT;         &lt; cfset go_thru_again = false&gt;     &LT; / CFIF&GT; &LT; / CFOUTPUT&GT; &LT; / CFLOOP&GT; &LT; /代码&GT;&LT; /预&GT;

2 个答案:

答案 0 :(得分:0)

如果源是回车分隔的textarea,您可以将文本区域视为chr(10)分隔的列表:

<cfset search_text_area = #form.overview_text#>  
<cfset length_of_text_field = #Len(search_text_area)#>
<cfset image_url_header = "https://ImagesFolder/subFolder/">
<cfset image_url_closure = '"'>
<cfset image_extension_png = ".png">      
<cfset image_list_array_on_submit =[]>
<cfset continue_searching_url = true>
<cfset first_pass = true>
<cfset go_thru_again = false>
<cfset counter = 0>

<cfloop from="1" to="#listlen(search_text_area,chr(10))#" index="linex">

<cfset thisrow=listgetat(search_text_area,linex,chr(10))>


<cfif findnocase(image_url_header,thisrow) gt 0>
    <cfif findnocase(image_url_closure,thisrow,findnocase(image_url_header,thisrow)) gt 0>
        <cfset starting_index_url = findnocase(image_url_header,thisrow)>                              
        <cfset ending_index_url = findnocase(image_url_closure,thisrow,findnocase(image_url_header,thisrow))>

        <cfset full_occurrence_url =  mid(search_text_area, (starting_index_url), ((ending_index_url)-starting_index_url))>
        <cfset local_occurrence_url = mid(search_text_area, (starting_index_url), ((ending_index_url)-starting_index_url))>
        <cfset ArrayAppend(image_list_array_on_submit, (local_occurrence_url))>

    </cfif>
</cfif>
</cfloop>

答案 1 :(得分:0)

我弄明白了,看看this code on trycf.com。我将在此发布以供参考。为了解决这个问题,我开始使用条件循环。在该循环内部,我使用IF语句在第一遍和后续传递之间切换。我检查某些条件退出循环。我的代码依赖于ColdFusion的find()和mid()函数来定位图像标记的源URL。

<cfsavecontent variable="SearchText">

Two images with source set to URL:
<p><img alt="Blah, Blah, Blah" src="https://ImagesFolder/subFolder/large-colored-bullets-3-0-14248.png" style="border-image:none; border:1px solid currentColor; float:left; height:200px; margin:12px; width:200px" /></p>
<p>and one more for testing:<img alt="something here" src="https://ImagesFolder/subFolder/goldmedal.png" style="border-image:none; border:1px solid currentColor; float:left; height:200px; margin:12px; width:200px" /></p>
</cfsavecontent>


<cfset search_text_area = #SearchText#>  
<cfset length_of_text_field = #Len(search_text_area)#>
<cfset image_url_header = "https://ImagesFolder/subFolder/">
<cfset image_url_closure = '"'>
<cfset image_extension_png = ".png">      
<cfset image_list_array_on_submit =[]>    
<cfset counter_array =[]>
<cfset continue_searching_url = true>
<cfset first_pass = true>
<cfset go_thru_again = false>
<cfset counter = 0>
<cfset next_position = -1>

<cfloop condition = "continue_searching_url eq true">
<cfoutput>
    <cfset counter = counter+1>
    <!---<cfif counter GTE 5>
        <cfbreak>
    </cfif>--->
    <cfif first_pass eq true>
        <cfset starting_index_url = find(image_url_header, SearchText)>
    </cfif>
    <cfif go_thru_again eq true>
        <cfset next_position = #ending_index_url#>
    </cfif>
    <cfif starting_index_url eq 0>
        <cfset continue_searching_url = false>
        <cfcontinue>
    </cfif>

    <cfif first_pass eq true>                             
        <cfset ending_index_url     = find( image_url_closure , SearchText, starting_index_url )>            
        <cfset full_occurrence_url  = mid(#SearchText#, (#starting_index_url#), ((#ending_index_url#)-#starting_index_url#))>
        <cfset local_occurrence_url = mid(#SearchText#, (#starting_index_url#), ((#ending_index_url#)-#starting_index_url#))>
        <cfset first_pass           = false>
        <cfset go_thru_again        = true>
        <cfset next_position        = #ending_index_url#>
        <cfset ArrayAppend(image_list_array_on_submit, (local_occurrence_url))>
    </cfif>
    <cfset ArrayAppend(counter_array, starting_index_url)>
    <cfif go_thru_again = true>
        <cfif starting_index_url NEQ length_of_text_field>

            <cfoutput><span style="color:brown;">#counter#. #starting_index_url#, #next_position#</span><br></cfoutput>


            <cfset starting_index_url   = find(image_url_header, SearchText, next_position)>
            <cfset ending_index_url     = find( image_url_closure , SearchText, starting_index_url )>            
            <cfif starting_index_url eq 0>
                <cfset continue_searching_url = false>
                <cfcontinue>
            </cfif>
            <cfset full_occurrence_url  = mid(#SearchText#, (#starting_index_url#), ((#ending_index_url#)-#starting_index_url#))>
            <cfset local_occurrence_url = mid(#SearchText#, (#starting_index_url#), ((#ending_index_url#)-#starting_index_url#))>
            <cfset first_pass           = false>
            <cfset go_thru_again        = true>
            <cfset next_position        = #ending_index_url#>
            <cfoutput><span style="color:blue;">#counter#. #starting_index_url#, #next_position#, #ending_index_url#</span><br></cfoutput>
            <cfset ArrayAppend(image_list_array_on_submit, (local_occurrence_url))>

        <cfelse>
            <cfset go_thru_again = false>
            <cfset starting_index_url = 0> 
            <cfcontinue>
        </cfif>

    </cfif>
</cfoutput>