이미지 url주소값만 변경하기
[code]
$search = '<img border="0" src="http://aaa.com/0.jpg" width="100" height="100">
<br><img border="0" src="http://aaa.com/1.jpg" width="100" height="100">
<br><img border="0" src="http://aaa.com/2.jpg" width="100" height="100">';
$replace = '<img border="0" src="위에서 추출한 이미주소 첫번째" width="100" height="100">
<br>안녕하세요~
<br><br>날씨가 많이 춥네요
<br><img border="0" src="위에서 추출한 이미주소 두번째" width="100" height="100">
<br>감기조심하세요
<br><img border="0" src="위에서 추출한 이미주소 세번째" width="100" height="100">';
class UrlChange {
var $matches, $i = 0, $regex = '/(?<=src=")[^"]+/', $val = '';
function __construct($search, $replace) {
preg_match_all($this->regex, $search, $matches);
$this->matches = $matches[0];
$this->val = preg_replace_callback($this->regex, array($this, 'call'), $replace);
}
function call($m) { return $this->matches[$this->i++]; }
}
$a = new UrlChange($search, $replace);
echo $a->val;
[/code]
댓글 9개
new UrlChange($search, $replace);
요게 출력되는 코드 같은데 요거로 배열? 값 이거로 담으려면 어떻게 해야하는지요?
즉)
$aaa = "출력값";
이런식으로요
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<?
$list = array(
array('1','img/000.jpg', 'img/000on.jpg', '100', '100'),
array('2','img/001.jpg', 'img/001on.jpg', '100', '100'),
array('3','img/002.jpg', 'img/002on.jpg', '100', '100')
);
?>
<?php foreach($list as $v){ ?>
<img border="0" id="img_0<?=$v[0]?>" src="<?=$v[1]?>" width="<?=$v[3]?>" height="<?=$v[4]?>" data-img="<?=$v[2]?>">
<? } ?>
<script>
$(document).ready(function(){
$("img").click(function(){
var o = $(this).attr('id');
var c = $("#"+o).attr('src');
$("#"+o).attr('src', $(this).data("img"));
$("#"+o).data("img",c);
})
});
</script>
[/code]
저두 코딩 드럽게 못하는데 대충 만들어봤습니다^^
클릭시 data-img 변경으로 전 이미지로 돌아가도록 구성했습니다.
<?php
$list = array(
array('img/000.jpg', '100', '100'),
array('img/001.jpg', '100', '100'),
array('img/002.jpg', '100', '100')
);
foreach($list as $v)
printf('<img src="%s" width="%s" height="%s" >', $v[0], $v[1], $v[2]);
?>
<script>
var img = document.getElementsByTagName('img');
for (var i=0; i<img.length; i++) {
(function(k){
img[k].onclick = function() {
this.src = this.src.indexOf('_on') + 1 ? this.src.replace('_on', '') : this.src.replace('.', '_on.');
}
})(i);
}
</script>
[/code]
<?php
$list = array(
array('img/000.jpg', '100', '100'),
array('img/001.jpg', '100', '100'),
array('img/002.jpg', '100', '100')
);
foreach($list as $v)
printf('<img src="%s" width="%s" height="%s" >', $v[0], $v[1], $v[2]);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$( 'img' ).on( 'click', function() {
this.src = this.src.indexOf('_on') + 1 ? this.src.replace('_on', '') : this.src.replace('.', '_on.');
});
</script>
[/code]
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4946 | node.js | 6년 전 | 2615 | ||
| 4945 | node.js | 6년 전 | 2388 | ||
| 4944 | node.js | 6년 전 | 2524 | ||
| 4943 | node.js | 6년 전 | 2276 | ||
| 4942 | node.js | 6년 전 | 2257 | ||
| 4941 | node.js | 6년 전 | 2723 | ||
| 4940 | node.js | 6년 전 | 1869 | ||
| 4939 | node.js | 6년 전 | 1997 | ||
| 4938 | node.js | 6년 전 | 2461 | ||
| 4937 | node.js | 6년 전 | 2253 | ||
| 4936 | node.js | 6년 전 | 2326 | ||
| 4935 | node.js | 6년 전 | 2139 | ||
| 4934 | node.js | 6년 전 | 2448 | ||
| 4933 | node.js | 6년 전 | 2251 | ||
| 4932 | node.js | 6년 전 | 2691 | ||
| 4931 | node.js | 6년 전 | 2075 | ||
| 4930 | node.js | 6년 전 | 2002 | ||
| 4929 | node.js | 6년 전 | 8638 | ||
| 4928 | node.js | 6년 전 | 3755 | ||
| 4927 | node.js | 6년 전 | 2398 | ||
| 4926 | node.js | 6년 전 | 2508 | ||
| 4925 | node.js | 6년 전 | 2091 | ||
| 4924 | node.js | 6년 전 | 3376 | ||
| 4923 | node.js | 6년 전 | 2227 | ||
| 4922 | node.js | 6년 전 | 1995 | ||
| 4921 | node.js | 6년 전 | 2050 | ||
| 4920 | node.js | 6년 전 | 1769 | ||
| 4919 | node.js | 6년 전 | 2037 | ||
| 4918 | node.js | 6년 전 | 2183 | ||
| 4917 | node.js | 6년 전 | 2398 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기