PHP echo使用空格截断URL路径

时间:2015-11-03 22:11:31

标签: php html

我有一个if / else处理可能有或没有https://值的数据,我无法控制数据库中的数据。除非$ row_WADAgbl_training ['url']数据网址中包含空格,否则此方法效果很好。回声似乎在空格示例的第一个实例之后截断了href URL:'filename space breaks.jpg'。我试图将它包装在urlencode(插入+符号)和htmlentities(截断空间出现的地方)中。当我在if / else之外使用常规href时,它工作正常。

作品:<a href="http://www.domain.com/training/<?php echo($row_WADAgbl_training['url']); ?>" target="_top"><?php echo($row_WADAgbl_training['url']); ?></a>

<?php
          if(substr($row_WADAgbl_training['url'],0,8)=="https://")
          {
             echo "<a target=_top href=".$row_WADAgbl_training['url'].">".$row_WADAgbl_training['url']."</a>"; //starts with http://, no formatting needed
          }
          else 
          {
             echo "<a target=_top href=https://www.domain.com/training/".htmlentities($row_WADAgbl_training['url']).">".$row_WADAgbl_training['url']."</a>";
          }
          ?>

2 个答案:

答案 0 :(得分:4)

您的HTML属性值must have quotes if they have a space in them

  <?php
  if(substr($row_WADAgbl_training['url'],0,8)=="https://")
  {
     echo "<a target=_top href='".$row_WADAgbl_training['url']."'>".$row_WADAgbl_training['url']."</a>"; //starts with http://, no formatting needed
  }
  else 
  {
     echo "<a target=_top href=https://www.domain.com/training/'".htmlentities($row_WADAgbl_training['url'])."'>".$row_WADAgbl_training['url']."</a>";
  }
  ?>

答案 1 :(得分:2)

您忘记在属性值中添加单引号,因为它们可能有空格(如您在问题中所述):

echo "<a target='_top' href='https://www.domain.com/training/".htmlentities($row_WADAgbl_training['url'])."'>".$row_WADAgbl_training['url']."</a>";

int main() {
    FILE *pFile = fopen("test.txt","r");
    int c, counter = 0;
    if (pFile != NULL) {
        while ((c = getc(pFile)) != EOF) {
            counter++;
            if (c != '\n') { putchar(c); }
            if (counter == 60) {
                counter = 0;
                printf("\n");
            }
        }
    }
    fclose(pFile);
}