PHP转义连接字符串

时间:2016-09-01 15:19:17

标签: php

我有一项看似简单的任务,但我有点挣扎。我知道我需要逃避引号,但我似乎无法使组合正确。

$referringURL = $_SERVER['HTTP_REFERER'];
echo "<a href = ".$referringURL./MyAccount/SearchUser.aspx" class = "back">Return to Search Users page</a>";

4 个答案:

答案 0 :(得分:1)

值得不时混合报价

echo '<a href = "' . $referringURL . '/MyAccount/SearchUser.aspx" class = "back">Return to Search Users page</a>';

答案 1 :(得分:1)

忘记转义,请使用 Heredoc

echo <<<HTML
<a href = "$referringURL/MyAccount/SearchUser.aspx" class = "back">
Return to Search Users page
</a>
HTML;

代码中的实际问题是在 ./ MyAccount 之前缺少",之后没有"

答案 2 :(得分:0)

玩得开心

BeanPropertyBindingResult result2 = new BeanPropertyBindingResult(incidentsForm, bindingResult.getObjectName();

    List<IncidentExtractStg> incidents= new ArrayList<IncidentExtractStg>();
    int i=0;// to get the row count
    int j=0;// to set the index 
    for(IncidentExtractStg ies : incidentsForm.getIncidents()) 
    {    
            int count=0;
            Field[] declaredFields = IncidentExtractStg.class.getDeclaredFields();
            for (Field field : declaredFields) 
            {
                if (bindingResult.hasFieldErrors("incidents["+i+"]."+field.getName()))
                {
                  for (FieldError error: bindingResult.getFieldErrors("incidents["+i+"]."+field.getName()))
                    {
                     result2.addError(new FieldError(error.getObjectName(), "incidents["+j+"]."+field.getName(), error.getRejectedValue(), error.isBindingFailure(), error.getCodes(), error.getArguments(), error.getDefaultMessage()));   
                    }
                 count++;
                }
            }

            if(count>0)
            {
                 j++;
                 incidents.add(ies);    
            }
            else 
            {
                ies.setValidated(1);
                incidentExtractStgService.update(ies);  
            }

        i++;
    }

    i=0;
    j=0;

    if (bindingResult.hasErrors()) {

        incidentsForm.setIncidents(incidents);
        System.out.println("error block");

        for (FieldError error: result2.getFieldErrors()) {
           System.out.println("field errors are  "+error.getField());
           System.out.println("field errors are  "+error);
        }

        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.incidentsForm", result2);
        redirectAttributes.addFlashAttribute("incidentsForm", incidentsForm);
        return "redirect:/validateIncidentList";
    }       

答案 3 :(得分:0)

为了避免引用问题,可以使用sprintf分隔主字符串和要插入的字符串:要插入的每个字符串由%s在主字符串中表示,然后添加为你有%s多个字符串:

<?php
$referringURL = $_SERVER['HTTP_REFERER'];
$s = sprintf( "<a href = '%s' class = '%s'>Return to Search Users page</a>",
              $referringURL . "/MyAccount/SearchUser.aspx",
              "back" );
echo $s;
?>

连接多个字符串时,此方法不那么混乱。